NLog.DiagnosticSource
6.1.4
Prefix Reserved
dotnet add package NLog.DiagnosticSource --version 6.1.4
NuGet\Install-Package NLog.DiagnosticSource -Version 6.1.4
<PackageReference Include="NLog.DiagnosticSource" Version="6.1.4" />
<PackageVersion Include="NLog.DiagnosticSource" Version="6.1.4" />
<PackageReference Include="NLog.DiagnosticSource" />
paket add NLog.DiagnosticSource --version 6.1.4
#r "nuget: NLog.DiagnosticSource, 6.1.4"
#:package NLog.DiagnosticSource@6.1.4
#addin nuget:?package=NLog.DiagnosticSource&version=6.1.4
#tool nuget:?package=NLog.DiagnosticSource&version=6.1.4
NLog.DiagnosticSource
NLog.DiagnosticSource extends NLog with support for System.Diagnostics.Activity and DiagnosticSource.
${activity}- NLog ActivityTraceLayoutRenderer for Microsoft Activity TraceDiagnosticListener- NLog DiagnosticListenerTarget for Microsoft DiagnosticSource
How to install
Install the package
Install-Package NLog.DiagnosticSourceor in your csproj:<PackageReference Include="NLog.DiagnosticSource" Version="6.*" />Add to your nlog.config:
<extensions> <add assembly="NLog.DiagnosticSource"/> </extensions>Alternative register from code using fluent configuration API:
LogManager.Setup().SetupExtensions(ext => { ext.RegisterTarget<NLog.Targets.DiagnosticListenerTarget>(); ext.RegisterLayoutRenderer<NLog.LayoutRenderers.ActivityTraceLayoutRenderer>(); });
How to use ActivityTraceLayoutRenderer
The System.Diagnostics.Activity.Current is the .NET abstraction used by OpenTelemetry for distributed tracing.
Example of NLog.config file that outputs TraceId information together with each LogEvent by using ${activity}:
<nlog>
<extensions>
<add assembly="NLog.DiagnosticSource"/>
</extensions>
<targets>
<target name="console" xsi:type="console" layout="${message}|ActivityId=${activity:property=TraceId}" />
</targets>
<rules>
<logger minLevel="Info" writeTo="console" />
</rules>
</nlog>
Property Enum Values
- Id : Hierarchical structure identifier that is concatenation of ParentIds
- SpanId : Identifier for the current activity (Ex. database activity within current request)
- ParentId : Identifier for the parent activity
- TraceId : Identifier for the root activity (Request Trace Identifier)
- OperationName : Operation name of the current activity
- DisplayName : Explicitly assigned activity display name (with fallback to OperationName)
- StartTimeUtc : Time when the operation started
- Duration : Duration of the operation (formatted as TimeSpan)
- DurationMs : Duration of the operation (formatted as TimeSpan.TotalMilliseconds)
- Baggage : Collection of key/value pairs that are passed to children of this Activity (Use
Format="@"for json-dictionary) - Tags : Collection of key/value pairs that are NOT passed to children of this Activity (Use
Format="@"for json-dictionary) - CustomProperty : Custom property assigned to this activity. Must be used together with Item-option
- Events : Events attached to this activity (Use
Format="@"for json-array) - TraceState : W3C tracestate header
- TraceFlags : See System.Diagnostics.ActivityTraceFlags for activity (defined by the W3C ID specification). For int-value combine with
format="d" - SourceName : Name of the activity source associated with this activity
- SourceVersion : Version of the activity source associated with this activity
- ActivityKind : Relationship kind between the activity, its parents, and its children. For int-value combine with
format="d" - TraceStateString : W3C 'tracestate' header as a string
- Status : Status code of the current activity (
OkorError). For int-value combine withformat="d" - StatusDescription : Status description of the current activity
- IsAllDataRequested : Activity span has been marked to be sampled / recorded (
0or1)
Formatting
- Format: Format for rendering the property.
- Culture: CultureInfo for rendering the property (Default Invariant Culture)
- Item: Lookup a single item from property-collection (Baggage, Tags, CustomProperty)
${activity:property=Baggage:item=BaggageKey}${activity:property=Tags:item=TagKey}${activity:property=CustomProperty:item=PropertyKey}
Extract property values from parent or root
You can also specify to extract the property from either the parent or the root activity.
${activity:property=OperationName:parent=true}
${activity:property=OperationName:root=true}
How to use DiagnosticListenerTarget
DiagnosticListenerTarget publishes NLog events as DiagnosticSource events, allowing subscribers such as diagnostics tools, telemetry libraries, or custom listeners to observe log events without coupling directly to NLog.
Example of NLog.config file that uses the diagnosticListener target:
<nlog>
<extensions>
<add assembly="NLog.DiagnosticSource"/>
</extensions>
<targets>
<target name="diagSource" xsi:type="diagnosticListener" layout="${message}" sourceName="nlog" eventName="${logger}" />
</targets>
<rules>
<logger minLevel="Info" writeTo="diagSource" />
</rules>
</nlog>
HostBuilder and ActivityTrackingOptions
The ${activity} layout renderer reads directly from Activity.Current. Microsoft.Extensions.Logging uses ActivityTrackingOptions
to control which activity details are included as logging scope properties:
builder.ConfigureLogging((hostingContext, loggingBuilder) =>
{
loggingBuilder.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId;
});
}).
SpanId / TraceId in custom NLog Target
NLog v5.3.4 introduced support for Layout.FromMethod that returns typed NLog Layout<T>. This allows custom NLog Targets to resolve SpanId / TraceId with support for AsyncWrapper, without having to parse from string or depend on NLog.DiagnosticSource nuget-package:
public class MyCustomTarget : TargetWithContext
{
private static readonly string EmptyTraceIdToHexString = default(System.Diagnostics.ActivityTraceId).ToHexString();
private static readonly string EmptySpanIdToHexString = default(System.Diagnostics.ActivitySpanId).ToHexString();
public Layout<System.Diagnostics.ActivityTraceId?> TraceId { get; set; } = Layout<System.Diagnostics.ActivityTraceId?>.FromMethod(static evt => System.Diagnostics.Activity.Current?.TraceId is System.Diagnostics.ActivityTraceId activityTraceId && !ReferenceEquals(EmptyTraceIdToHexString, activityTraceId.ToHexString()) ? activityTraceId : null);
public Layout<System.Diagnostics.ActivitySpanId?> SpanId { get; set; } = Layout<System.Diagnostics.ActivitySpanId?>.FromMethod(static evt => System.Diagnostics.Activity.Current?.SpanId is System.Diagnostics.ActivitySpanId activitySpanId && !ReferenceEquals(EmptySpanIdToHexString, activitySpanId.ToHexString()) ? activitySpanId : null);
protected override void Write(LogEventInfo logEvent)
{
var spanId = RenderLogEvent(SpanId, logEvent);
if (spanId.HasValue)
data.SpanId = spanId.Value;
var traceId = RenderLogEvent(TraceId, logEvent);
if (traceId.HasValue)
data.TraceId = traceId.Value;
}
}
W3C is the recommended format for distributed tracing and is the default on modern .NET versions. .NET Framework continues to default to hierarchical IDs unless configured otherwise.
- System.Diagnostics.Activity.DefaultIdFormat = System.Diagnostics.ActivityIdFormat.W3C;
- or by setting the environment variable DOTNET_SYSTEM_DIAGNOSTICS_ACTIVITY_DEFAULTIDFORMAT to W3C
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 was computed. 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 is compatible. 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. |
-
.NETFramework 4.6.2
- NLog (>= 6.1.4)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
-
.NETStandard 2.0
- NLog (>= 6.1.4)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
-
net10.0
- NLog (>= 6.1.4)
-
net6.0
- NLog (>= 6.1.4)
-
net8.0
- NLog (>= 6.1.4)
NuGet packages (37)
Showing the top 5 NuGet packages that depend on NLog.DiagnosticSource:
| Package | Downloads |
|---|---|
|
Ark.Tools.NLog
NLog configuration helper and extensions for Ark standard configuration using code and not config files. |
|
|
Ark.Tools.Solid
SOLID base for Handler pattern |
|
|
Ark.Tools.ResourceWatcher
Watcher over listable resources with support for change detection, retry limit and state. |
|
|
Ark.Tools.FtpClient.Core
Core abstractions for FtpClient |
|
|
Ark.Tools.Sql.SqlServer
Extensions and helpers of Ark.Tools.Sql for SqlServer |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on NLog.DiagnosticSource:
| Repository | Stars |
|---|---|
|
NLog/NLog
NLog - Flexible and Structured Logging for various .NET Platforms
|
| Version | Downloads | Last Updated |
|---|---|---|
| 6.1.4 | 0 | 7/12/2026 |
| 6.0.3 | 505,748 | 11/14/2025 |
| 6.0.2 | 226,865 | 7/20/2025 |
| 6.0.1 | 25,366 | 6/28/2025 |
| 6.0.0 | 17,142 | 6/23/2025 |
| 5.2.3 | 427,607 | 6/15/2025 |
| 5.2.2 | 106,169 | 5/14/2025 |
| 5.2.1 | 2,613,498 | 1/28/2024 |
| 5.2.0 | 454,740 | 10/15/2023 |
| 5.0.0 | 1,094,562 | 11/22/2022 |
| 1.3.0 | 452,497 | 7/29/2022 |
| 1.2.0 | 509,727 | 9/28/2021 |
| 1.1.0 | 93,990 | 7/1/2021 |
| 1.0.2 | 155,716 | 3/10/2021 |
| 1.0.1 | 35,274 | 1/13/2021 |
| 1.0.0 | 18,622 | 1/7/2021 |
- Updated NLog v6.1.4
See https://github.com/NLog/NLog.DiagnosticSource for documentation of ${activity} and DiagnosticListenerTarget