Aore.Logger.SQLite
1.2.1
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Aore.Logger.SQLite --version 1.2.1
NuGet\Install-Package Aore.Logger.SQLite -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aore.Logger.SQLite" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aore.Logger.SQLite" Version="1.2.1" />
<PackageReference Include="Aore.Logger.SQLite" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aore.Logger.SQLite --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Aore.Logger.SQLite, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Aore.Logger.SQLite@1.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aore.Logger.SQLite&version=1.2.1
#tool nuget:?package=Aore.Logger.SQLite&version=1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Aore.Logger.SQLite — SQLite 日志扩展包
Aore.Logger 的 SQLite 数据库日志目标:零部署、单文件即库,适合单机应用、嵌入式场景与集成测试。
1. 作用与目的
把日志同步写入 SQLite 数据库表,支持:
- 自动建表/建索引:目标构造时自动
CREATE TABLE IF NOT EXISTS+ 时间戳/级别两个索引,无需手工 DDL; - 结构探测 fail-fast:表已存在但缺少已映射列时构造期抛错(不会静默丢日志);
- 字段映射:9 个字段(含 UTC 时间列)的列名逐字段可配、可置 null 跳过,支持写入现有表;
- 保留清理:可选按天保留,后台分批删除(每批 5000 行),不阻塞写入;
- 参数化写入:消息/异常全部经驱动参数绑定,无 SQL 注入面。
2. 安装与依赖
dotnet add package Aore.Logger.SQLite
- 依赖驱动:Microsoft.Data.Sqlite 8.0.29(自动引入)
- 必须同时引用核心包
Aore.Logger - 目标框架:netstandard2.0 / net462 / net472 / net48 / net8.0
3. 快速上手
using Aore.Logger;
Logger.Instance.Configure(new LoggerConfig
{
EnableFileLog = false, // 也可 true 双写
EnableDatabaseLog = true,
DatabaseLogConfig = new DatabaseLogConfig
{
DatabaseType = DatabaseType.SQLite,
ConnectionString = "Data Source=D:/logs/app.db", // 建议绝对路径
TableName = "Logs"
}
});
Logger.Instance.Info("写入 SQLite 的日志");
连接失败/建表失败在 Configure 时抛出(保留旧目标列表并带内层异常),不会等到写日志才发现。
4. 自动创建的表与索引
CREATE TABLE IF NOT EXISTS "Logs" (
"Id" TEXT PRIMARY KEY NOT NULL, -- Guid 字符串(36 位)
"Timestamp" TEXT NOT NULL, -- 'yyyy-MM-dd HH:mm:ss.fff' 文本,毫秒保留
"Level" TEXT NOT NULL, -- 'INFO'/'ERROR'/...
"Message" TEXT NOT NULL,
"Exception" TEXT,
"MemberName" TEXT,
"FilePath" TEXT,
"LineNumber" INTEGER,
"createdGMT" TEXT NOT NULL -- UTC 时间(Timestamp 转换)
);
-- 另按已映射列创建 Timestamp 与 Level 两个索引
5. 连接字符串
- 常规:
Data Source=app.db - 相对路径基于进程当前工作目录(不是程序目录),生产环境建议绝对路径;
Data Source=:memory:可用于集成测试——注意内存库生命周期与连接绑定,且每条日志独立借还连接,需配合驱动池化语义理解其局限(建议;Pooling=False时明确知晓每条日志拿到的是不同内存库)。
6. 清理(保留策略)
IsClearingExecuted = true 启用:写入时命中周期窗口(小时数 % ExecuteClearingCycle == 0)派发后台清理,按已映射的时间戳列删除早于 RetentionDays 的行,按 rowid 分批(5000 行/批)循环删除——不依赖 Id 列,自增主键现有表(IdColumn = null)清理照常工作。
7. 写入现有表(字段映射)
把列名置 null 表示不写该字段,映射到现有列名即写入现有表:
DatabaseLogConfig = new DatabaseLogConfig
{
DatabaseType = DatabaseType.SQLite,
ConnectionString = "Data Source=app.db",
TableName = "sys_oper_log",
IdColumn = null, // 自增主键表必须置 null
TimestampColumn = "created_at",
LevelColumn = "log_level",
MessageColumn = "content",
MemberNameColumn = null // 无此列,跳过
}
完整规则(三态语义、校验、类型兼容矩阵)见 数据库日志通用手册。
8. 本方言注意事项
- 时间戳为固定格式文本(
yyyy-MM-dd HH:mm:ss.fff):字符串比较即为时间序,但按时间范围查询需用同格式字面量或datetime()函数; - 标识符以双引号转义,表名/列名仅允许
字母/数字/下划线/$(违规构造期抛InvalidOperationException); - 排障可用
new SQLiteLogTarget(config).TestConnection()探测连通性; - SQLite 单写者模型:多进程同时写同一库文件可能遇到
database is locked(该条被 Logger 吞掉)。高频并发写建议改用客户端/服务端数据库(MySQL/PostgreSQL)。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.2
- Aore.Logger (>= 1.2.1)
- Microsoft.Data.Sqlite (>= 8.0.29)
-
.NETFramework 4.7.2
- Aore.Logger (>= 1.2.1)
- Microsoft.Data.Sqlite (>= 8.0.29)
-
.NETFramework 4.8
- Aore.Logger (>= 1.2.1)
- Microsoft.Data.Sqlite (>= 8.0.29)
-
.NETStandard 2.0
- Aore.Logger (>= 1.2.1)
- Microsoft.Data.Sqlite (>= 8.0.29)
-
net8.0
- Aore.Logger (>= 1.2.1)
- Microsoft.Data.Sqlite (>= 8.0.29)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
SQLite logging extension for Aore.Logger