NLog.DiagnosticSource 5.2.1

Prefix Reserved
dotnet add package NLog.DiagnosticSource --version 5.2.1                
NuGet\Install-Package NLog.DiagnosticSource -Version 5.2.1                
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="NLog.DiagnosticSource" Version="5.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NLog.DiagnosticSource --version 5.2.1                
#r "nuget: NLog.DiagnosticSource, 5.2.1"                
#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.
// Install NLog.DiagnosticSource as a Cake Addin
#addin nuget:?package=NLog.DiagnosticSource&version=5.2.1

// Install NLog.DiagnosticSource as a Cake Tool
#tool nuget:?package=NLog.DiagnosticSource&version=5.2.1                

NLog.DiagnosticSource

NLog ActivityTraceLayoutRenderer for Microsoft Activity Trace

NLog DiagnosticListenerTarget for Microsoft DiagnosticSource

Version AppVeyor

How to install

  1. Install the package

    Install-Package NLog.DiagnosticSource or in your csproj:

    <PackageReference Include="NLog.DiagnosticSource" Version="5.*" />
    
  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

  • 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 : Explicit assigned Activity DisplayName (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). Can be combined 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. Can be combined with format="d"
  • TraceStateString : W3C 'tracestate' header as a string

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

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>
Product 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 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.

atisu.services.common

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on NLog.DiagnosticSource:

Repository Stars
NLog/NLog
NLog - Advanced and Structured Logging for Various .NET Platforms
Version Downloads Last updated
5.2.1 608,678 1/28/2024
5.2.0 229,857 10/15/2023
5.0.0 801,737 11/22/2022
1.3.0 270,912 7/29/2022
1.2.0 461,124 9/28/2021
1.1.0 86,475 7/1/2021
1.0.2 144,391 3/10/2021
1.0.1 33,569 1/13/2021
1.0.0 7,295 1/7/2021

- Added NET8 as target-platform

See https://github.com/NLog/NLog.DiagnosticSource for documentation of ${activity} and DiagnosticListenerTarget