Hyz.Trace.Logging.NLog
1.0.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.NLog --version 1.0.0
NuGet\Install-Package Hyz.Trace.Logging.NLog -Version 1.0.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.NLog" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyz.Trace.Logging.NLog" Version="1.0.0" />
<PackageReference Include="Hyz.Trace.Logging.NLog" />
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.NLog --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hyz.Trace.Logging.NLog, 1.0.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.NLog@1.0.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.NLog&version=1.0.0
#tool nuget:?package=Hyz.Trace.Logging.NLog&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hyz.Trace.Logging.NLog
Hyz.Trace 的 NLog 日志集成 — 提供自定义 LayoutRenderer,将链路追踪的 TraceId 和 SpanId 渲染到日志输出中,实现日志与追踪的关联。
安装
dotnet add package Hyz.Trace.Logging.NLog
本包不包含 Hyz.Trace 核心库,需配合以下任一包使用:
Hyz.Trace.Client(客户端元包,推荐)Hyz.Trace(仅核心库)
功能
| LayoutRenderer | 占位符 | 说明 |
|---|---|---|
TraceIdLayoutRenderer |
${hyz-trace-id} |
渲染当前链路的 TraceId |
SpanIdLayoutRenderer |
${hyz-span-id} |
渲染当前链路的 SpanId |
使用示例
方式一:链式 API 注册(推荐)
NLog Fluent Setup 链式调用
using NLog;
using Hyz.Trace.Logging.NLog;
// 应用启动时,一行链式调用完成注册
LogManager.Setup()
.UseHyzTrace() // 一键注册 TraceId/SpanId LayoutRenderer
.LoadConfigurationFromFile("nlog.config");
简单静态方法注册
using Hyz.Trace.Logging.NLog;
// 应用启动时调用(幂等,线程安全,多次调用无副作用)
NLogTraceExtensions.RegisterHyzTraceLayoutRenderers();
方式二:手动注册(传统方式)
using NLog;
using NLog.Config;
using Hyz.Trace.Logging.NLog;
// 在应用启动时手动注册
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("hyz-trace-id", typeof(TraceIdLayoutRenderer));
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("hyz-span-id", typeof(SpanIdLayoutRenderer));
配置 NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console"
layout="${longdate} | ${level:uppercase=true} | ${hyz-trace-id} | ${hyz-span-id} | ${message} ${exception:format=tostring}" />
<target name="file" xsi:type="File"
fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} | ${level:uppercase=true} | ${hyz-trace-id} | ${hyz-span-id} | ${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="console,file" />
</rules>
</nlog>
输出效果
2026-07-14 14:30:15.1234 | INFO | abc123def456 | span789 | User login successful
2026-07-14 14:30:15.2345 | ERROR | abc123def456 | span790 | Database connection failed
API 参考
NLogTraceExtensions 静态类
| 方法 | 说明 |
|---|---|
RegisterHyzTraceLayoutRenderers() |
幂等注册 LayoutRenderer,线程安全,可多次调用 |
UseHyzTrace(this ISetupBuilder) |
NLog Fluent Setup 链式扩展方法,返回 ISetupBuilder 支持继续链式配置 |
与元包的关系
Hyz.Trace.Client 元包不包含本包(日志集成属于服务端组件)。如需 NLog 日志追踪能力,请显式安装本包。
依赖
- NLog 5.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.2.1)
- Hyz.Trace (>= 1.0.0)
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- NLog (>= 5.4.0)
- System.Diagnostics.DiagnosticSource (>= 8.0.1)
- System.Text.Json (>= 10.0.6)
-
net10.0
- Castle.Core (>= 5.2.1)
- Hyz.Trace (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- NLog (>= 5.4.0)
-
net8.0
- Castle.Core (>= 5.2.1)
- Hyz.Trace (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- NLog (>= 5.4.0)
-
net9.0
- Castle.Core (>= 5.2.1)
- Hyz.Trace (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- NLog (>= 5.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.