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:

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

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