AsyncLoggerLib 1.0.0

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

📦 AsyncLoggerLib —— 高并发 SQLite 日志系统

NuGet Version License: MIT

适用于 .NET Framework 4.8 及以上版本的高并发日志记录库,使用 SQLite 存储日志,支持:

  • ✅ 按天分库、按小时分表
  • ✅ 自定义属性(JSON 序列化)
  • ✅ 系统属性作为独立字段存储(线程 ID、进程 ID、设备名等)
  • ✅ 异步/同步写入 + 缓存机制
  • ✅ 支持配置路径和日志目录自定义
  • ✅ 自动清理历史日志
  • ✅ 查询服务:等级筛选、全文检索、正则匹配、排序、分页
  • ✅ 支持 ASP.NET Core 注册为服务

🚀 快速开始

安装 NuGet 包

dotnet add package AsyncLoggerLib

🔧 使用方法

命令行 / 控制台应用示例

using AsyncLoggerLib;

class Program
{
    static void Main(string[] args)
    {
        // 初始化日志系统(自动加载 appsettings.json)
        LoggerBootstrapper.Initialize();

        // 写入日志
        Logger.Info("应用程序已启动");
        Logger.Warn("这是一个警告信息");
        Logger.Error("发生了一个错误", new Exception("测试异常"));

        Console.WriteLine("日志已写入,按任意键退出...");
        Console.ReadKey();

        // 关闭日志系统
        LoggerBootstrapper.Shutdown();
    }
}

自定义配置路径和日志目录

// 指定配置文件路径 + 日志存储目录
LoggerBootstrapper.Initialize(
    configPath: @"C:\MyApp\config\logger.json",
    logDirectory: @"D:\MyApp\logs"
);

📄 目录结构

默认日志文件会保存在程序根目录下的 Logs/ 文件夹中:

Logs/
├── 2025-04-05.db
│   ├── Logs_00
│   ├── Logs_01
│   └── ...

⚙️ 配置说明(appsettings.json)

{
  "LoggerOptions": {
    "BatchSize": 50,
    "FlushIntervalSeconds": 2,
    "MaxRetries": 3,
    "MaxDaysToKeep": 7,

    "IncludeThreadId": true,
    "IncludeProcessId": true,
    "IncludeMachineName": true,
    "IncludeProcessName": true,
    "IncludeThreadName": true
  }
}
配置项 描述
BatchSize 批量写入条数
FlushIntervalSeconds 写入间隔(秒)
MaxRetries 写入失败最大重试次数
MaxDaysToKeep 最大保留天数
Include* 是否记录系统属性

🔍 日志查询服务(ILogQueryService)

var queryService = new SqliteLogQueryService(dbFilePath);

var options = new LogQueryOptions
{
    LevelFilter = LogLevelFilter.Warn | LogLevelFilter.Error,
    FromTime = DateTime.Now.AddDays(-1),
    ToTime = DateTime.Now,
    FullTextSearchTerm = "error",
    OrderByTimestampDescending = true,
    PageNumber = 1,
    PageSize = 20
};

var logs = await queryService.QueryLogsAsync(options);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 was computed.  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 Framework net48 is compatible.  net481 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.0.0 209 5/12/2025

- 初始版本发布
     - 支持 SQLite 存储日志
     - 支持高并发写入 + 缓存机制
     - 支持日志查询服务
     - 支持 ASP.NET Core 注册为服务