Hyz.Trace.Logging.Serilog 0.9.0

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

Hyz.Trace.Logging.Serilog

Hyz.Trace 的 Serilog 日志集成 — 提供 TraceEnricher,自动将链路追踪的 TraceId 和 SpanId 注入到日志事件中,实现日志与追踪的关联。

安装

dotnet add package Hyz.Trace.Logging.Serilog

本包不包含 Hyz.Trace 核心库,需配合以下任一包使用:

  • Hyz.Trace.Client(客户端元包,推荐)
  • Hyz.Trace(仅核心库)

功能

TraceEnricher 实现 Serilog 的 ILogEventEnricher 接口,自动为每条日志添加以下属性:

属性名 说明
TraceId 当前链路的追踪 ID
SpanId 当前链路的 Span ID

使用示例

方式一:链式 API 注册(推荐)

using Serilog;
using Hyz.Trace.Logging.Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.WithHyzTrace()  // 一键添加链路追踪上下文
    .WriteTo.Console(outputTemplate: 
        "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{TraceId}] [{SpanId}] {Message}{NewLine}{Exception}")
    .WriteTo.File("logs/log-.txt",
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{TraceId}] [{SpanId}] {Message}{NewLine}{Exception}",
        rollingInterval: RollingInterval.Day)
    .CreateLogger();

方式二:手动实例化(传统方式)

using Serilog;
using Hyz.Trace.Logging.Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.With(new TraceEnricher())  // 手动添加 TraceEnricher
    .WriteTo.Console(outputTemplate: 
        "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{TraceId}] [{SpanId}] {Message}{NewLine}{Exception}")
    .WriteTo.File("logs/log-.txt",
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{TraceId}] [{SpanId}] {Message}{NewLine}{Exception}",
        rollingInterval: RollingInterval.Day)
    .CreateLogger();

输出效果

2026-07-14 14:30:15.123 [INF] [abc123def456] [span789] User login successful
2026-07-14 14:30:15.234 [ERR] [abc123def456] [span790] Database connection failed

与 ASP.NET Core 集成

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.WithHyzTrace()      // 链式 API
    .Enrich.FromLogContext()
    .WriteTo.Console());

API 参考

SerilogTraceExtensions 静态类

方法 说明
WithHyzTrace(this LoggerEnrichmentConfiguration) Serilog Fluent Configuration 链式扩展方法,添加 TraceEnricher 到日志管道,返回 LoggerConfiguration 支持继续链式配置

与元包的关系

Hyz.Trace.Client 元包不包含本包(日志集成属于服务端组件)。如需 Serilog 日志追踪能力,请显式安装本包。

依赖

  • Serilog 3.0.0+(自动作为传递依赖引入)
  • Hyz.Trace 核心库(需使用者自行引用)

目标框架

netstandard2.0net8.0net9.0net10.0 — 兼容所有支持 .NET Standard 2.0 及 .NET 8+ 的运行时。

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

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.9.0 0 7/31/2026
0.8.0 0 7/31/2026
0.7.0 0 7/31/2026
0.6.0 0 7/31/2026
0.5.0 55 7/30/2026
0.4.0 82 7/29/2026
0.3.0 77 7/28/2026
0.2.0 80 7/27/2026
0.1.0 96 7/14/2026