Aore.Logger.Postgre
1.2.2
dotnet add package Aore.Logger.Postgre --version 1.2.2
NuGet\Install-Package Aore.Logger.Postgre -Version 1.2.2
<PackageReference Include="Aore.Logger.Postgre" Version="1.2.2" />
<PackageVersion Include="Aore.Logger.Postgre" Version="1.2.2" />
<PackageReference Include="Aore.Logger.Postgre" />
paket add Aore.Logger.Postgre --version 1.2.2
#r "nuget: Aore.Logger.Postgre, 1.2.2"
#:package Aore.Logger.Postgre@1.2.2
#addin nuget:?package=Aore.Logger.Postgre&version=1.2.2
#tool nuget:?package=Aore.Logger.Postgre&version=1.2.2
Aore.Logger.Postgre — PostgreSQL 日志扩展包
Aore.Logger 的 PostgreSQL 数据库日志目标:Linux/云原生环境首选,MVCC 并发友好,原生 UUID 与 TIMESTAMP(3) 类型。
1. 作用与目的
把日志同步写入 PostgreSQL 数据库表,支持:
- 自动建表/建索引:构造时自动建表(
"双引号"转义标识符)+ 时间戳/级别索引; - 原生类型:
UUID主键、TIMESTAMP(3)毫秒时间戳; - 池化客户端:Npgsql 连接池,多进程集中写入友好;
- 字段映射:9 个字段列名逐字段可配、可置 null 跳过,支持写入现有表;
- 保留清理:可选按天保留,后台按
ctid分批删除(每批 5000 行),不阻塞写入。
2. 安装与依赖
dotnet add package Aore.Logger.Postgre
- 依赖驱动:Npgsql 7.0.10(固定 7.x——Npgsql 8+ 不再支持 netstandard2.0)
- 必须同时引用核心包
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.Postgre,
ConnectionString = "Host=192.168.1.30;Port=5432;Database=applogs;Username=loguser;Password=******",
TableName = "Logs"
}
});
Logger.Instance.Info("写入 PostgreSQL 的日志");
连接串也支持 URI 形式:postgresql://loguser:******@192.168.1.30/applogs。连接失败/建表失败在 Configure 时抛出(保留旧目标列表并带内层异常)。
4. 自动创建的表与索引
CREATE TABLE IF NOT EXISTS "Logs" (
"Id" UUID NOT NULL PRIMARY KEY, -- Guid
"Timestamp" TIMESTAMP(3) NOT NULL, -- 毫秒精度
"Level" VARCHAR(50) NOT NULL,
"Message" TEXT NOT NULL,
"Exception" TEXT,
"MemberName" VARCHAR(500),
"FilePath" VARCHAR(1000),
"LineNumber" INTEGER,
"createdGMT" TIMESTAMP(3) NOT NULL -- UTC 时间(Timestamp 转换)
);
-- 另按已映射列创建 Timestamp 与 Level 两个索引
5. 清理(保留策略)
IsClearingExecuted = true 启用:写入时命中周期窗口派发后台清理,按已映射的时间戳列删除早于 RetentionDays 的行;按 ctid 分批(5000 行/批)循环删除——短事务、避免长事务膨胀、不依赖 Id 列(GENERATED ... AS IDENTITY 表 IdColumn = null 清理照常)。
6. 写入现有表(字段映射)
DatabaseLogConfig = new DatabaseLogConfig
{
DatabaseType = DatabaseType.Postgre,
ConnectionString = "...",
TableName = "sys_oper_log",
IdColumn = null, // GENERATED ALWAYS AS IDENTITY 表必须置 null!
TimestampColumn = "created_at",
LevelColumn = "log_level",
MessageColumn = "content"
}
⚠️ GENERATED ALWAYS AS IDENTITY 列必须 IdColumn = null(显式写自增列直接报错,每条写入失败且被 Logger 吞掉)。
7. 本方言注意事项
- 索引名上限 63 字符:超长自动截断 + FNV-1a 哈希后缀;
- 标识符以
"双引号"转义(区分大小写),表名/列名仅允许字母/数字/下划线/$; default(DateTime)(0001-01-01)超出TIMESTAMP语义范围,Timestamp请使用正常业务时间;- 排障可用
new PostgreLogTarget(config).TestConnection()探测连通性; - 连接串支持
Maximum Pool Size、SSL Mode等 Npgsql 扩展键。
| 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)
- Npgsql (>= 7.0.10)
-
.NETFramework 4.7.2
- Aore.Logger (>= 1.2.2)
- Npgsql (>= 7.0.10)
-
.NETFramework 4.8
- Aore.Logger (>= 1.2.2)
- Npgsql (>= 7.0.10)
-
.NETStandard 2.0
- Aore.Logger (>= 1.2.2)
- Npgsql (>= 7.0.10)
-
net8.0
- Aore.Logger (>= 1.2.2)
- Npgsql (>= 7.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
PostgreSQL logging extension for Aore.Logger