Hyz.Trace.Logging.Serilog
0.9.0
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
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" />
<PackageReference Include="Hyz.Trace.Logging.Serilog" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Hyz.Trace.Logging.Serilog&version=0.9.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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.0、net8.0、net9.0、net10.0 — 兼容所有支持 .NET Standard 2.0 及 .NET 8+ 的运行时。
| Product | Versions 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.
-
.NETStandard 2.0
- Castle.Core (>= 5.1.1)
- Hyz.Trace (>= 0.9.0)
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Serilog (>= 3.1.1)
- System.Diagnostics.DiagnosticSource (>= 8.0.1)
- System.Text.Json (>= 10.0.6)
-
net10.0
- Castle.Core (>= 5.1.1)
- Hyz.Trace (>= 0.9.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Serilog (>= 3.1.1)
-
net8.0
- Castle.Core (>= 5.1.1)
- Hyz.Trace (>= 0.9.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Serilog (>= 3.1.1)
-
net9.0
- Castle.Core (>= 5.1.1)
- Hyz.Trace (>= 0.9.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Serilog (>= 3.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.