LyuLogExtension 1.6.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package LyuLogExtension --version 1.6.1
                    
NuGet\Install-Package LyuLogExtension -Version 1.6.1
                    
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="LyuLogExtension" Version="1.6.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LyuLogExtension" Version="1.6.1" />
                    
Directory.Packages.props
<PackageReference Include="LyuLogExtension" />
                    
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 LyuLogExtension --version 1.6.1
                    
#r "nuget: LyuLogExtension, 1.6.1"
                    
#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 LyuLogExtension@1.6.1
                    
#: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=LyuLogExtension&version=1.6.1
                    
Install as a Cake Addin
#tool nuget:?package=LyuLogExtension&version=1.6.1
                    
Install as a Cake Tool

LyuLogExtension

NuGet

基于 ZLogger 的日志扩展库。

安装

dotnet add package LyuLogExtension

使用方式

方式一:依赖注入(ASP.NET Core / Host)

using LogExtension.Builder;
using LogExtension.Extensions;
using Microsoft.Extensions.Logging;

services.AddZLogger(builder => builder
    // 文件输出配置
    .AddFileOutput("logs/trace/", minLevel:LogLevel.Trace, maxLevel:LogLevel.Debug)  // Trace + Debug
    .AddFileOutput("logs/info/", LogLevel.Information)              // Info 及以上
    .AddFileOutput("logs/error/", LogLevel.Error)                   // Error 及以上
    .AddFileOutput("logs/debug/", LogLevel.debug, null, RollingInterval.Hour, 2048) //debug使用独立滚动配置
    
    // 控制台输出
    .WithConsole()              // 带时间戳
    // .WithConsoleDetails()    // 带时间戳和类名
    
    // 过滤器
    .FilterMicrosoft()          // 过滤 Microsoft 命名空间 (Warning+)
                    			//.FilterMicrosoft(LogLevel.Information) 
    .FilterSystem()             // 过滤 System 命名空间 (Warning+)
    .WithFilter("MyApp.Verbose", LogLevel.Warning)  // 自定义过滤
    
    // 滚动配置(全局默认)
    .WithRollingInterval(RollingInterval.Day)  // 按天滚动
    .WithRollingSizeKB(4096)                   // 单文件最大 4MB
);

方式二:静态方式(控制台应用 / 无 DI 场景)

using LogExtension;
using LogExtension.Builder;
using Microsoft.Extensions.Logging;

// 配置
ZLogFactory.Configure(builder => builder
    .AddFileOutput("logs/trace/", LogLevel.Trace, LogLevel.Debug)
    .AddFileOutput("logs/info/", LogLevel.Information)
    .WithConsole()
    .FilterMicrosoft()
);

// 获取 Logger
var logger = ZLogFactory.Get<Program>();
logger.ZLogInformation($"应用启动");

方式三:从配置文件加载

services.AddZLogger(configuration, "ZLogger");

// 配置文件 + 链式覆盖
services.AddZLogger(configuration, builder => builder
    .WithConsole()
    .FilterMicrosoft(),
    "ZLogger"
);

appsettings.json 示例:

{
  "ZLogger": {
    "GlobalRollingInterval": "Day",
    "GlobalRollingSizeKB": 4096,
    "Outputs": [
      {
        "Path": "logs/trace/",
        "MinLevel": "Trace",
        "MaxLevel": "Debug"
      },
      {
        "Path": "logs/info/",
        "MinLevel": "Information"
      },
      {
        "Path": "logs/error/",
        "MinLevel": "Error"
      }
    ],
    "LogLevel": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  }
}

文件输出配置

快捷方法

.AddTraceOutput()           // logs/trace/ (Trace + Debug)
.AddTraceOutput("custom/")  // 自定义路径

.AddInfoOutput()            // logs/ (Info+)
.AddInfoOutput("custom/")   // 自定义路径

.AddErrorOutput()           // logs/error/ (Error+)
.AddErrorOutput("custom/")  // 自定义路径

滚动配置

// 全局默认配置(对所有未单独配置的输出生效)
.WithRollingInterval(RollingInterval.Day)   // Hour / Day / Month / Year
.WithRollingSizeKB(4096)                    // 单文件最大大小 KB

// 单个输出独立配置(覆盖全局)
.AddFileOutput("logs/error/", LogLevel.Error, null, RollingInterval.Hour, 2048)

默认值:

  • 滚动间隔:Hour(每小时)
  • 单文件大小:2048 KB(2MB)

控制台配置

.WithConsole()          // 时间戳格式:2025-01-01 12:00:00.000 [INF] 消息
.WithConsoleDetails()   // 详细格式:2025-01-01 12:00:00.000 [INF] [MyApp.Service] 消息

过滤器配置

// 快捷方法
.FilterMicrosoft()                          // Microsoft 命名空间 Warning+
.FilterMicrosoft(LogLevel.Error)            // Microsoft 命名空间 Error+
.FilterSystem()                             // System 命名空间 Warning+
.FilterSystem(LogLevel.Error)               // System 命名空间 Error+

// 自定义过滤
.WithFilter("MyApp.Verbose", LogLevel.Warning)
.WithFilter("System.Net.Http", LogLevel.Error)

// 批量过滤
.WithFilters(new Dictionary<string, LogLevel>
{
    ["Microsoft"] = LogLevel.Warning,
    ["System"] = LogLevel.Warning,
    ["MyApp.Debug"] = LogLevel.Information
})

日志记录

// 注入方式
public class MyService(ILogger<MyService> logger)
{
    public void DoWork()
    {
        logger.ZLogTrace($"跟踪信息");
        logger.ZLogDebug($"调试信息: {value}");
        logger.ZLogInformation($"普通信息");
        logger.ZLogWarning($"警告信息");
        logger.ZLogError($"错误信息");
        logger.ZLogCritical($"严重错误");
        
        // 带异常
        try { ... }
        catch (Exception ex)
        {
            logger.ZLogError(ex, $"操作失败: {operation}");
        }
    }
}

// 静态方式
var logger = ZLogFactory.Get<Program>();
logger.ZLogInformation($"消息");

⚠️ 注意:必须使用 $"" 字符串插值语法,否则会编译报错。

日志输出格式

控制台:

2025-01-01 12:00:00.000 [INF] 应用启动成功
2025-01-01 12:00:00.001 [WRN] 配置缺失

文件:

2025-01-01 12:00:00.000 [INF] [MyApp.Services.UserService:42] 用户登录成功
2025-01-01 12:00:00.001 [ERR] [MyApp.Controllers.ApiController:78] 数据库连接失败
异常: System.InvalidOperationException: Connection timeout
堆栈: at MyApp.Controllers.ApiController.GetData() in ApiController.cs:line 78

默认配置

配置项 默认值
输出路径 logs/
日志级别 Trace 及以上(全部)
滚动间隔 每小时
单文件大小 2MB
控制台 关闭
过滤器

链式方法一览

方法 说明
AddFileOutput(path, min, max?) 添加文件输出
AddFileOutput(path, min, max, interval, size) 添加文件输出(带滚动配置)
AddTraceOutput(path?) 快捷:Trace + Debug
AddInfoOutput(path?) 快捷:Info 及以上
AddErrorOutput(path?) 快捷:Error 及以上
WithRollingInterval(interval) 全局滚动间隔
WithRollingSizeKB(size) 全局单文件大小
WithConsole() 启用控制台(时间戳)
WithConsoleDetails() 启用控制台(时间戳+类名)
WithFilter(category, level) 添加过滤器
WithFilters(dict) 批量添加过滤器
FilterMicrosoft(level?) 过滤 Microsoft 命名空间
FilterSystem(level?) 过滤 System 命名空间
FromConfiguration(config, section?) 从配置文件加载
WithAdditionalConfiguration(action) 额外 ILoggingBuilder 配置

致谢

本项目基于 ZLogger 构建。

License

MIT

Product Compatible and additional computed target framework versions.
.NET 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 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. 
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.7.2 101 1/6/2026
1.7.1 291 12/18/2025
1.7.0 210 12/15/2025
1.6.2 189 12/5/2025
1.6.1 182 12/5/2025
1.5.1 200 11/26/2025
1.5.0 193 11/26/2025
1.4.0 192 11/26/2025
1.3.3 331 11/17/2025
1.3.2 327 11/17/2025
1.3.1 315 11/17/2025
1.2.1 326 11/17/2025
1.0.4 181 11/15/2025
1.0.3 180 11/15/2025
1.0.1 196 11/15/2025