NLog log correlation
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes. The Elastic APM .NET agent enables you to trace the execution of operations in your .NET applications, sending performance metrics and errors to the Elastic APM server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless Observability
Generally available
Stack
Generally available
APM Agent .NET
Generally available
For NLog, we offer two LayoutRenderers that inject the current trace and transaction id into logs.
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}.
Rather than using a Layout Renderer such as jsonlayout, you may specify the Trace and Transaction ID in the Target Layout:
<nlog>
<extensions>
<add assembly="Elastic.Apm.NLog"/>
</extensions>
<targets>
<target name="console"
type="console"
layout="${ElasticApmTraceId}|${ElasticApmTransactionId}|${ElasticApmSpanId}|${message}" />
</targets>
<rules>
<logger name="*" minLevel="Debug" writeTo="Console" />
</rules>
</nlog>