Aore.Logger.SqlServer
1.2.2
dotnet add package Aore.Logger.SqlServer --version 1.2.2
NuGet\Install-Package Aore.Logger.SqlServer -Version 1.2.2
<PackageReference Include="Aore.Logger.SqlServer" Version="1.2.2" />
<PackageVersion Include="Aore.Logger.SqlServer" Version="1.2.2" />
<PackageReference Include="Aore.Logger.SqlServer" />
paket add Aore.Logger.SqlServer --version 1.2.2
#r "nuget: Aore.Logger.SqlServer, 1.2.2"
#:package Aore.Logger.SqlServer@1.2.2
#addin nuget:?package=Aore.Logger.SqlServer&version=1.2.2
#tool nuget:?package=Aore.Logger.SqlServer&version=1.2.2
Aore.Logger.SqlServer — SQL Server 日志扩展包
Aore.Logger 的 SQL Server 数据库日志目标:Windows 生态首选,NVARCHAR 中文安全、DATETIME2 强类型时间。
1. 作用与目的
把日志同步写入 SQL Server 数据库表,支持:
- 自动建表/建索引:构造时自动建表(
[方括号]转义标识符)+ 时间戳/级别索引; - 强类型时间:
DATETIME2(3)列 +SqlDbType.DateTime2参数,毫秒精度、无字符串转换; - Unicode 安全:文本列统一 NVARCHAR(N 前缀参数),中文消息不乱码;
- 字段映射:9 个字段列名逐字段可配、可置 null 跳过,支持写入现有表;
- 保留清理:可选按天保留,后台
DELETE TOP (5000)分批删除,不阻塞写入。
2. 安装与依赖
dotnet add package Aore.Logger.SqlServer
- 依赖驱动:Microsoft.Data.SqlClient 6.1.6(自动引入)
- 必须同时引用核心包
Aore.Logger - 目标框架:netstandard2.0 / net462 / net472 / net48 / net8.0
3. 快速上手
using Aore.Logger;
Logger.Instance.Configure(new LoggerConfig
{
EnableFileLog = false,
EnableDatabaseLog = true,
DatabaseLogConfig = new DatabaseLogConfig
{
DatabaseType = DatabaseType.SqlServer,
ConnectionString = "Server=192.168.1.20;Database=applogs;User Id=loguser;Password=******;TrustServerCertificate=true",
TableName = "Logs"
}
});
Logger.Instance.Info("写入 SQL Server 的日志");
Windows 集成认证:Server=.;Database=applogs;Trusted_Connection=True;。连接失败/建表失败在 Configure 时抛出(保留旧目标列表并带内层异常)。
Microsoft.Data.SqlClient 4.0+ 默认
Encrypt=True,自签名证书环境需加TrustServerCertificate=true,否则构造期即报证书信任错误。
4. 自动创建的表与索引
CREATE TABLE [Logs] (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, -- Guid
[Timestamp] DATETIME2(3) NOT NULL, -- 毫秒精度,强类型参数
[Level] NVARCHAR(50) NOT NULL,
[Message] NVARCHAR(MAX) NOT NULL,
[Exception] NVARCHAR(MAX),
[MemberName] NVARCHAR(500),
[FilePath] NVARCHAR(1000),
[LineNumber] INT,
[createdGMT] DATETIME2(3) NOT NULL -- UTC 时间(Timestamp 转换)
);
-- 另按已映射列创建 Timestamp 与 Level 两个索引
5. 清理(保留策略)
IsClearingExecuted = true 启用:写入时命中周期窗口派发后台清理,按已映射的时间戳列删除早于 RetentionDays 的行;DELETE TOP (5000) 循环删到无行可删——短事务、不依赖 Id 列(IDENTITY 自增表 IdColumn = null 清理照常)。
6. 写入现有表(字段映射)
DatabaseLogConfig = new DatabaseLogConfig
{
DatabaseType = DatabaseType.SqlServer,
ConnectionString = "...",
TableName = "sys_oper_log",
IdColumn = null, -- IDENTITY(1,1) 表必须置 null!
TimestampColumn = "created_at",
LevelColumn = "log_level",
MessageColumn = "content"
}
⚠️ IDENTITY(1,1) 列必须 IdColumn = null(未开 IDENTITY_INSERT 时显式写自增列直接报错,每条写入失败且被 Logger 吞掉)。
7. 本方言注意事项
- 索引名上限 128 字符:超长自动截断 + FNV-1a 哈希后缀;
- 标识符以
[方括号]转义,表名/列名仅允许字母/数字/下划线/$; default(DateTime)(0001-01-01)超出DATETIME2语义范围,Timestamp请使用正常业务时间;- 排障可用
new SqlServerLogTarget(config).TestConnection()探测连通性; - 连接串支持
Maximum Pool Size(默认池化开启),高频写日志场景可适当调大。
| 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. |
-
.NETFramework 4.6.2
- Aore.Logger (>= 1.2.2)
- Microsoft.Data.SqlClient (>= 6.1.6)
-
.NETFramework 4.7.2
- Aore.Logger (>= 1.2.2)
- Microsoft.Data.SqlClient (>= 6.1.6)
-
.NETFramework 4.8
- Aore.Logger (>= 1.2.2)
- Microsoft.Data.SqlClient (>= 6.1.6)
-
.NETStandard 2.0
- Aore.Logger (>= 1.2.2)
- Microsoft.Data.SqlClient (>= 6.1.6)
-
net8.0
- Aore.Logger (>= 1.2.2)
- Microsoft.Data.SqlClient (>= 6.1.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
SQL Server logging extension for Aore.Logger