| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
NLog.DiagnosticSource extends NLog with support for System.Diagnostics.Activity and DiagnosticSource.
${activity} - NLog ActivityTraceLayoutRenderer for Microsoft Activity Trace
DiagnosticListener - NLog DiagnosticListenerTarget for Microsoft DiagnosticSource
Install the package
Install-Package NLog.DiagnosticSource or 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>();
});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
Formatting
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}
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>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;
});
}).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.
| Back | FazBrowse Home | New Git URL |