LumLog 1.1.0

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

LumLog

面向生产环境的极简异步文件日志库,实现 Microsoft ILogger 抽象,支持高并发写入、按天滚动、写日志回调扩展。

目标框架: .NET Standard 2.0 · .NET 8.0 · .NET 9.0 · .NET 10.0

特性

  • 极简设计:核心代码量少,无反射,依赖轻量
  • 跨平台兼容:单 NuGet 包同时支持 .NET Framework 4.6.1+、.NET Core / .NET 5+ 及 .NET 8 / 9 / 10
  • 异步高并发:基于 System.Threading.Channels,生产者不阻塞,后台线程串行写盘
  • 文件句柄缓存:同一路径复用 StreamWriter,减少 IO 开销
  • 按天滚动:日志文件 {BaseDirectory}/log/{directory}/{yyyyMMdd}.log
  • Microsoft 抽象:实现 ILoggerILoggerProviderILoggerFactory
  • 全局配置:运行时总开关、最低日志级别、手动 Flush
  • 写日志回调:独立 Channel 异步执行,可用于 SQL 同步、远程推送;回调出错不影响主日志
  • AOT 友好(net8.0 / net9.0 / net10.0):适合 Native AOT 场景

安装

dotnet add package LumLog

或 NuGet 包管理器:

Install-Package LumLog

框架兼容性

NuGet 包内置四个目标,按消费者项目自动匹配最优版本:

lib/netstandard2.0/LumLog.dll
lib/net8.0/LumLog.dll
lib/net9.0/LumLog.dll
lib/net10.0/LumLog.dll
消费者项目 使用的 LumLog 目标 说明
.NET Framework 4.6.1+ netstandard2.0 经典桌面 / Web 项目
.NET Core 2.0+ netstandard2.0
.NET 5 / 6 / 7 netstandard2.0
.NET 8 net8.0
.NET 9 net9.0
.NET 10 net10.0
Native AOT net8.0 / net9.0 / net10.0 需以对应版本为目标框架

关于 AOT

netstandard2.0 是 API 契约,不是运行时,不能作为 AOT 发布目标。AOT 项目引用 LumLog 时,NuGet 会自动选用 net8.0 / net9.0 / net10.0 版本参与原生编译。LumLog 无反射,在对应 TFM 下可正常 AOT 发布。

快速开始

方式一:配合 DI / Host(推荐)

using Lum.Log;
using Microsoft.Extensions.Logging;

// Program.cs / Startup.cs
builder.Logging.AddProvider(new LogProvider("myApp"));

// 业务代码 — 使用标准 ILogger 抽象
public class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public void Run() => _logger.LogInformation("服务启动");
}

方式二:独立使用 LogFactory(不依赖 DI)

using Lum.Log;
using Microsoft.Extensions.Logging;

using (var factory = new LogFactory("myApp"))
{
    ILogger logger = factory.CreateLogger("MyClass");
    logger.LogWarning("独立运行");
}

方式三:.NET Framework 示例

using Lum.Log;
using Microsoft.Extensions.Logging;

var factory = new LogFactory("myApp");
ILogger logger = factory.CreateLogger("MyClass");
logger.LogError("发生错误");
LogHelper.Flush(); // 应用退出前手动刷新

方式四:Native AOT(.NET 8 / 9 / 10)


<PropertyGroup>
  <TargetFramework>net10.0</TargetFramework>
  <PublishAot>true</PublishAot>
</PropertyGroup>
using Lum.Log;
using Microsoft.Extensions.Logging;

var factory = new LogFactory("myApp");
ILogger logger = factory.CreateLogger("MyApp");
logger.LogInformation("AOT 发布");
LogHelper.Flush();

日志路径与格式

项目 说明
根目录 {AppDomain.CurrentDomain.BaseDirectory}log\
子目录 构造 LogProvider("myApp")LogFactory("myApp") 时传入
文件名 {yyyyMMdd}.log,按天滚动
示例路径 D:\app\log\myApp\20260605.log

单条日志格式:

[2026-06-05 14:30:00.1234] [Information] (MyService) 服务启动

配置

以下均为 LogHelper 上的全局静态配置,对所有 Logger 实例生效。

using Lum.Log;
using Microsoft.Extensions.Logging;

// 全局总开关(false = 不写文件、不触发回调,IsEnabled 恒为 false)
LogHelper.Enabled = true;

// 最低日志级别(低于此级别的日志被忽略)
// Debug 编译默认 Trace;Release 编译默认 Information
LogHelper.MinLevel = LogLevel.Warning;

// 手动刷新缓冲区(不关闭日志引擎,适合退出前或关键操作后)
LogHelper.Flush();
API 说明
LogHelper.Enabled 全局总开关,等价于"开启/关闭所有 LumLog 日志"
LogHelper.MinLevel 最低日志级别,运行时随时可调
ILogger.IsEnabled 综合 EnabledMinLevel 判断
LogHelper.Flush() 将 Channel 中剩余日志刷入文件

EnabledMinLevel 的关系:

  • Enabled = false → 所有级别均不记录
  • Enabled = true → 由 MinLevel 决定过滤,如设为 Warning 则 Information 及以下被忽略

进程正常退出时会自动 flush 并关闭日志引擎。

写日志回调

回调是 LumLog 的扩展能力,不在 ILogger 标准接口上。在应用启动时注册一次,即可监听所有经 LumLog 写出的日志。

注意

  • 回调只覆盖 LumLog 自身的输出;若 Host 中还注册了 Console、Debug 等其他 Provider,那些日志不会进入回调
  • 回调注册在 LogHelper 静态 API 上,与业务层使用的 ILogger 抽象无关
using Lum.Log;

LogWriteCallback myCallback = (message, logLevel, category, logDirectory, logFileName) =>
{
    // message       — 完整格式化日志行
    // logLevel      — 如 "Information"
    // category      — 如 "MyService"
    // logDirectory  — 目录,含末尾 \,如 D:\app\log\myApp\
    // logFileName   — 如 20260605.log

    // 示例:写入数据库 / 推送远程
    // var fullPath = logDirectory + logFileName;
    // SaveToDb(message);
};

LogHelper.RegisterWriteCallback(myCallback);

// 取消注册
LogHelper.UnregisterWriteCallback(myCallback);

ILogger 配合使用:

builder.Logging.AddProvider(new LogProvider("myApp"));
LogHelper.RegisterWriteCallback(myCallback);   // 启动时注册

// 业务层只用 ILogger,无需引用 LumLog
logger.LogInformation("hello");  // 写文件 + 触发回调

回调在独立后台线程执行;若回调抛出异常,错误信息会写入主日志,不影响文件写入。

禁用日志

// 方式一:全局关闭(推荐,可随时恢复)
LogHelper.Enabled = false;

// 方式二:使用空实现(IsEnabled 恒为 false,零开销)
ILogger logger = LogHelper.InvalidLog;

API 概览

类型 说明
LogProvider ILoggerProvider,注册到 ILoggingBuilder / ILoggerFactory
LogFactory ILoggerFactory,独立使用,按 category 缓存 Logger
LogHelper ILogger 实现;静态配置与回调注册入口
LogHelper.InvalidLog 空 Logger,不写任何日志
LogWriteCallback 写日志回调委托

LogFactory.AddProvider 不支持添加外部 Provider;多 Provider 场景请使用 LogProvider + DI。

依赖

版本 说明
Microsoft.Extensions.Logging.Abstractions 9.0.9 所有目标均引用
System.Threading.Channels 8.0.0 仅 netstandard2.0 传递依赖
System.Text.Encoding.CodePages 8.0.0 仅 netstandard2.0 传递依赖

net8.0 / net9.0 / net10.0 目标使用对应运行时内置实现,无额外传递依赖。

要求

目标 用途
.NET Standard 2.0 .NET Framework 4.6.1+、.NET Core 2.0+ 等
.NET 8.0 现代 .NET,支持 Native AOT
.NET 9.0 现代 .NET,支持 Native AOT
.NET 10.0 现代 .NET,支持 Native AOT

许可证

MIT License — 见 LICENSE.txt

链接

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 is compatible.  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 is compatible.  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 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on LumLog:

Package Downloads
LumWebManager

use with: { "info": { "title": "xxxApp", "logo": "FlowApplication" }, "url": "https://127.0.0.1:10505", "isSwaggerAllowed": true, "isCorsAllowed": true, "isLogAllowed": true, "isHttpLogging": true, "isJWTEnabled": true, "isHttpsApplied": true, "assemblies": [], "jwt": { "secretKey": "111", "issuer": "fff", "audience": "users", "expireInSecond": 3601 }, "https": { "pfx_name": "www.xxx", "pfx_pswd": "x8x9mdvm", "server_port": 443 } }

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 45 6/5/2026
1.0.0 268 10/13/2025