LyuLogExtension 1.0.4
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package LyuLogExtension --version 1.0.4
NuGet\Install-Package LyuLogExtension -Version 1.0.4
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.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LyuLogExtension" Version="1.0.4" />
<PackageReference Include="LyuLogExtension" />
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.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LyuLogExtension, 1.0.4"
#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.0.4
#: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.0.4
#tool nuget:?package=LyuLogExtension&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
LogExtension
基于 ZLogger 高性能的日志简易扩展库,内置简单配置的日志记录功能,支持工厂模式和依赖注入两种使用方式。
特性
- 🚀 双模式支持:支持工厂模式和依赖注入两种使用方式
- 📝 自动日志分级:Trace/Debug 和 Info 及以上级别分别输出到不同文件
- 🔄 滚动日志:按小时自动滚动,单文件最大 2MB
- 🧵 线程安全:支持多线程并发日志记录
- 📍 调用位置追踪:自动记录类名和行号
- ⚡ 高性能:基于 ZLogger 的高性能日志框架
依赖项
依赖Zlogger 感谢Zlogger研发团队 : https://github.com/Cysharp/ZLogger
使用方式
方式一:工厂模式(静态使用)
适用于不使用依赖注入的场景,如控制台应用、类库等。
基本用法
using LogExtension;
// 获取日志记录器
var logger = ZlogFactory.Get<Program>();
// 记录日志
logger.LogInformation("应用启动");
logger.LogDebug("调试信息: {Value}", 42);
logger.LogWarning("警告信息");
logger.LogError(exception, "发生错误");
自定义工厂
如果需要自定义日志配置,可以设置自己的 LoggerFactory:
using Microsoft.Extensions.Logging;
using ZLogger;
// 创建自定义工厂
var customFactory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddZLoggerConsole();
logging.AddZLoggerFile("logs/custom.log");
});
// 设置全局工厂
ZlogFactory.SetFactory(customFactory);
// 之后所有通过 ZlogFactory.Get<T>() 获取的 logger 都会使用自定义配置
var logger = ZlogFactory.Get<MyClass>();
方式二:依赖注入(DI)
适用于 ASP.NET Core、Worker Service 等支持依赖注入的场景。
注册服务
在 Program.cs 或 Startup.cs 中注册日志服务:
using LogExtension;
var builder = WebApplication.CreateBuilder(args);
// 添加 ZLogger 日志服务(使用默认配置)
builder.Services.AddZLogger();
var app = builder.Build();
使用自定义配置
using LogExtension;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
// 创建自定义工厂
var customFactory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Information);
logging.AddZLoggerConsole();
});
// 使用自定义工厂注册
builder.Services.AddZLogger(customFactory);
var app = builder.Build();
在类中注入使用
public class MyService
{
private readonly ILogger<MyService> _logger;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
}
public void DoWork()
{
_logger.LogInformation("开始执行任务");
_logger.LogDebug("处理数据: {Count}", 100);
_logger.LogInformation("任务完成");
}
}
在控制器中使用
[ApiController]
[Route("api/[controller]")]
public class WeatherController : ControllerBase
{
private readonly ILogger<WeatherController> _logger;
public WeatherController(ILogger<WeatherController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Get()
{
_logger.LogInformation("获取天气数据");
return Ok(new { Temperature = 25 });
}
}
日志输出
默认配置
- Trace/Debug 日志:输出到
logs/trace/目录 - Info 及以上日志:输出到
logs/目录 - 滚动策略:每小时自动滚动,单文件超过 2MB 时自动创建新文件
- 文件名格式:
yyyy-MM-dd-HH_001.log
日志格式
2024-11-15 14:30:25.123 [INF] [MyNamespace.MyClass:42] 这是日志消息
2024-11-15 14:30:26.456 [ERR] [MyNamespace.MyClass:45] 发生错误
异常: System.InvalidOperationException: 操作无效
堆栈: at MyNamespace.MyClass.Method() in C:\Path\To\File.cs:line 45
格式说明:
- 时间戳(本地时间)
- 日志级别(3 字符缩写:TRC/DBG/INF/WRN/ERR/CRT)
- 类名和行号
- 日志消息
- 异常信息(如果有)
日志级别
| 级别 | 说明 | 输出位置 |
|---|---|---|
| Trace | 最详细的跟踪信息 | logs/trace/ |
| Debug | 调试信息 | logs/trace/ |
| Information | 一般信息性消息 | logs/ |
| Warning | 警告信息 | logs/ |
| Error | 错误信息 | logs/ |
| Critical | 严重错误 | logs/ |
使用结构化日志
// 推荐:使用占位符
logger.LogInformation("用户 {UserId} 下载了文件 {FileName},大小: {FileSize} bytes",
userId, fileName, fileSize);
// 不推荐:字符串拼接
logger.LogInformation($"用户 {userId} 下载了文件 {fileName},大小: {fileSize} bytes");
| Product | Versions 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 | 96 | 1/6/2026 |
| 1.7.1 | 289 | 12/18/2025 |
| 1.7.0 | 208 | 12/15/2025 |
| 1.6.2 | 186 | 12/5/2025 |
| 1.6.1 | 179 | 12/5/2025 |
| 1.5.1 | 199 | 11/26/2025 |
| 1.5.0 | 191 | 11/26/2025 |
| 1.4.0 | 191 | 11/26/2025 |
| 1.3.3 | 327 | 11/17/2025 |
| 1.3.2 | 324 | 11/17/2025 |
| 1.3.1 | 312 | 11/17/2025 |
| 1.2.1 | 322 | 11/17/2025 |
| 1.0.4 | 180 | 11/15/2025 |
| 1.0.3 | 179 | 11/15/2025 |
| 1.0.1 | 193 | 11/15/2025 |