Hyz.Trace.Logging.NLog 1.0.0

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" />
                    
Directory.Packages.props
<PackageReference Include="Hyz.Trace.Logging.NLog" />
                    
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.NLog --version 1.0.0
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=Hyz.Trace.Logging.NLog&version=1.0.0
                    
Install as a Cake Tool

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.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
1.0.0 0 7/31/2026
0.9.0 0 7/31/2026
0.8.0 0 7/31/2026
0.7.0 32 7/31/2026
0.6.0 39 7/31/2026
0.5.0 37 7/30/2026
0.4.0 63 7/29/2026
0.3.0 78 7/28/2026
0.2.0 87 7/27/2026
0.1.0 97 7/14/2026