Aore.Logger.MySql 1.2.2

dotnet add package Aore.Logger.MySql --version 1.2.2
                    
NuGet\Install-Package Aore.Logger.MySql -Version 1.2.2
                    
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.MySql" Version="1.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aore.Logger.MySql" Version="1.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Aore.Logger.MySql" />
                    
Project file
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.MySql --version 1.2.2
                    
#r "nuget: Aore.Logger.MySql, 1.2.2"
                    
#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.MySql@1.2.2
                    
#: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.MySql&version=1.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Aore.Logger.MySql&version=1.2.2
                    
Install as a Cake Tool

Aore.Logger.MySql — MySQL 日志扩展包

Aore.Logger 的 MySQL 数据库日志目标:服务端中心化存储,适合单机到中小规模集群的日志集中查询。

1. 作用与目的

把日志同步写入 MySQL 数据库表,支持:

  • 自动建表/建索引:构造时自动建表(反引号转义标识符)+ 时间戳/级别索引;
  • 毫秒精度时间戳:DATETIME(3) 列,毫秒完整保留(见 §8);
  • 字段映射:9 个字段列名逐字段可配、可置 null 跳过,支持写入现有表(含自增主键表);
  • 保留清理:可选按天保留,后台 DELETE ... LIMIT 分批删除(每批 5000 行),不阻塞写入;
  • 参数化写入:全部值经驱动参数绑定,无 SQL 注入面。

2. 安装与依赖

dotnet add package Aore.Logger.MySql
  • 依赖驱动:MySqlConnector 2.6.1(自动引入,兼容 MySQL 5.7/8.x 与 MariaDB)
  • 必须同时引用核心包 Aore.Logger
  • 目标框架:netstandard2.0 / net462 / net472 / net48 / net8.0

3. 快速上手

using Aore.Logger;

Logger.Instance.Configure(new LoggerConfig
{
    EnableFileLog = true,                // 文件 + 数据库双写
    EnableDatabaseLog = true,
    DatabaseLogConfig = new DatabaseLogConfig
    {
        DatabaseType = DatabaseType.MySql,
        ConnectionString = "Server=localhost;Port=3306;Database=appdb;User Id=root;Password=******",
        TableName = "Logs"
    }
});

Logger.Instance.Info("写入 MySQL 的日志");

连接失败/建表失败在 Configure 时抛出(保留旧目标列表并带内层异常)。

4. 自动创建的表与索引

CREATE TABLE IF NOT EXISTS `Logs` (
    `Id` CHAR(36) NOT NULL,                   -- Guid 字符串(36 位)主键
    `Timestamp` DATETIME(3) NOT NULL,         -- 毫秒精度
    `Level` VARCHAR(50) NOT NULL,
    `Message` TEXT NOT NULL,
    `Exception` TEXT,
    `MemberName` VARCHAR(500),
    `FilePath` VARCHAR(1000),
    `LineNumber` INT,
    `createdGMT` DATETIME(3) NOT NULL,        -- UTC 时间(Timestamp 转换)
    PRIMARY KEY (`Id`)
);
-- 另按已映射列创建 Timestamp 与 Level 两个索引

5. 连接字符串

MySqlConnector 格式:Server=;Port=;Database=;User Id=;Password=,支持 Maximum Pool Size、SslMode、Connection Timeout 等扩展键。建议为日志库单独建账号并只授予目标库权限。

6. 清理(保留策略)

IsClearingExecuted = true 启用:写入时命中周期窗口派发后台清理,按已映射的时间戳列删除早于 RetentionDays 的行;DELETE ... LIMIT 5000 循环删到无行可删——短事务、不长时间锁表、不依赖 Id 列(自增主键表 IdColumn = null 清理照常)。

7. 写入现有表(字段映射)

DatabaseLogConfig = new DatabaseLogConfig
{
    DatabaseType = DatabaseType.MySql,
    ConnectionString = "...",
    TableName = "sys_oper_log",
    IdColumn = null,                      // AUTO_INCREMENT 表必须置 null!
    TimestampColumn = "created_at",
    LevelColumn = "log_level",
    MessageColumn = "content"
}

⚠️ 自增主键表必须 IdColumn = null,否则本库会显式向自增列写值导致每条 INSERT 失败且被 Logger 吞掉(静默丢日志)。

8. 本方言注意事项

  1. 时间戳以 yyyy-MM-dd HH:mm:ss.fff 字符串发送到 DATETIME(3) 列:规避驱动往返对毫秒的截断,毫秒完整保留;查询条件同样用字符串字面量即可;
  2. default(DateTime)(0001-01-01)会写丢:MySQL DATETIME 下限为 1000-01-01,超范围时间戳该条静默丢失——Timestamp 请使用正常业务时间;
  3. 索引名上限 64 字符:超长自动截断 + FNV-1a 哈希后缀;
  4. 标识符以反引号转义,表名/列名仅允许 字母/数字/下划线/$;
  5. 排障可用 new MySqlLogTarget(config).TestConnection() 探测连通性。
Product 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2 72 9/28/2026
1.2.1 78 9/25/2026
1.2.0 93 9/25/2026 1.2.0 is deprecated because it is no longer maintained.

MySql logging extension for Aore.Logger