This is a cache of https://www.elastic.co/guide/en/apm/agent/dotnet/current/nlog.html. It is a snapshot of the page at 2024-09-05T01:02:30.400+0000.
NLog | APM .NET Agent Reference [1.x] | Elastic

NLog

edit

For NLog, we offer two LayoutRenderers that inject the current trace and transaction id into logs.

In order to use them, you need to add the Elastic.Apm.NLog NuGet package to your application and load it in the <extensions> section of your NLog config file:

<nlog>
<extensions>
   <add assembly="Elastic.Apm.NLog"/>
</extensions>
<targets>
<target type="file" name="logfile" fileName="myfile.txt">
    <layout type="jsonlayout">
        <attribute name="traceid" layout="${ElasticApmTraceId}" />
        <attribute name="transactionid" layout="${ElasticApmTransactionId}" />
    </layout>
</target>
</targets>
<rules>
    <logger name="*" minLevel="Trace" writeTo="logfile" />
</rules>
</nlog>

As you can see in the sample file above, you can reference the current transaction id with ${ElasticApmTransactionId} and the trace id with ${ElasticApmTraceId}.