Lzq.Extensions.Serilog 0.2.0

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

Lzq.Extensions.Serilog

一个基于 Serilog 的 ASP.NET Core 日志扩展库,提供开箱即用的 TraceId 追踪、HTTP 请求上下文富化、多 Sink 灵活配置,并完全交由使用者通过 SerilogOptions 控制日志行为。

Lzq.Core

🌟 核心特性

  • TraceId 自动附加:通过 ActivityTraceIdEnricherActivity.Current.TraceId 附加到每条日志,便于分布式追踪。
  • HTTP 请求上下文富化HttpRequestEnricher 自动添加当前 HTTP 请求的 MethodPath 到日志属性。
  • 统一输出模板:所有文本型 Sink(控制台、文件)共用同一个可配置的输出模板。
  • 多 Sink 支持
    • 控制台(支持异步、自定义模板或 JSON 格式)
    • 文件(支持滚动策略、大小限制、保留数量)
    • SQLite(本地数据库存储)
    • Grafana Loki(集中式日志聚合)
  • 完全配置驱动:所有 Sink 的启用/禁用及参数均由 SerilogOptions 控制,不依赖环境变量判断。
  • 与 ASP.NET Core 无缝集成:通过 AddLzqSerilog 扩展方法一键替换默认日志提供程序。

🚀 快速开始

var builder = WebApplication.CreateBuilder(args);
// 使用默认配置(控制台开启,输出模板为带时间的文本格式)
builder.AddLzqSerilog();

// 或者
builder.AddLzqSerilog(options =>
{
    options.MinimumLevel = Serilog.Events.LogEventLevel.Information;
    options.EnableFile = true;
    options.FilePath = "Logs/myapp-.log";
    options.FileSizeLimitBytes = 20 * 1024 * 1024; // 20 MB
    options.EnableLoki = true;
    options.LokiUrl = "http://loki:3100";
    options.LokiServiceName = "MyAppService";
});

全局设置

属性 类型 默认值 说明
MinimumLevel LogEventLevel Debug 全局最低日志级别
OutputTemplate string? "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [TraceId:{TraceId}] {Message:lj}{NewLine}{Exception}" 统一输出模板,控制台和文件共用

控制台输出配置

属性 类型 默认值 说明
EnableConsole bool true 是否启用控制台输出
ConsoleAsync bool true 是否对控制台输出使用异步包装(提高性能)

文件输出配置

属性 类型 默认值 说明
EnableFile bool false 是否启用文件输出
FilePath string? null 日志文件路径(如 Logs/log-.txt
FileRollingInterval RollingInterval Day 文件滚动间隔
FileSizeLimitBytes long 10 * 1024 * 1024 单个文件大小上限(字节)
RetainedFileCountLimit int 7 保留的文件数量(按滚动周期)
FileAsync bool true 是否对文件输出使用异步包装

SQLite 输出配置

属性 类型 默认值 说明
EnableSQLite bool false 是否启用 SQLite 日志输出
SQLitePath string? "Logs/log.db" SQLite 数据库文件路径

Loki 输出配置

属性 类型 默认值 说明
EnableLoki bool false 是否启用 Loki 日志推送
LokiUrl string? null Loki 服务地址
LokiServiceName string "unknown_service" Loki 标签中的服务名称
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 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. 
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
0.2.0 38 5/19/2026
0.0.9 45 5/17/2026
0.0.8 44 5/17/2026
0.0.5 39 5/17/2026