A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/NLog/NLog.DiagnosticSource below:

NLog/NLog.DiagnosticSource: NLog integration with Microsoft Activity TraceId and SpanId

NLog ActivityTraceLayoutRenderer for Microsoft Activity Trace

NLog DiagnosticListenerTarget for Microsoft DiagnosticSource

  1. Install the package

    Install-Package NLog.DiagnosticSource or in your csproj:

    <PackageReference Include="NLog.DiagnosticSource" Version="6.*" />
  2. 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 from Microsoft allows one to create OpenTelemetry spans.

Example of NLog.config file that outputs span-details together with 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

It is possible to specify that the above property should be extracted from either root- or parent-activity.

${activity:property=OperationName:parent=true}
${activity:property=OperationName:root=true}

Manually configure ActivityTrackingOptions

When using the default HostBuilder then it will automatically setup the following ActivityTrackingOptions:

builder.ConfigureLogging((hostingContext, loggingBuilder) =>
{
      loggingBuilder.Configure(options =>
      {
            options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
                                            | ActivityTrackingOptions.TraceId
                                            | ActivityTrackingOptions.ParentId;
      });
}).

If creating a custom HostBuilder, then one have to manually setup the ActivityTrackingOptions like shown above.

How to use DiagnosticListenerTarget

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>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4