APM serilog enricher
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. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. 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.
Serverless
Generally available
Stack
Generally available
This enricher adds the transaction id and trace id to every Serilog log message that is created during a transaction.
Add a reference to the Elastic.Apm.SerilogEnricher package:
<PackageReference Include="Elastic.Apm.SerilogEnricher" Version="8.6.0" />
var logger = new LoggerConfiguration()
.Enrich.WithElasticApmCorrelationInfo()
.WriteTo.Console(outputTemplate: "[{ElasticApmTraceId} {ElasticApmTransactionId} {ElasticApmSpanId} {Message:lj} {NewLine}{Exception}")
.CreateLogger();
In the code snippet above Enrich.WithElasticApmCorrelationInfo() enables the enricher from this project, which will set 3 properties for log lines that are created during a transaction:
ElasticApmTraceIdElasticApmTransactionIdElasticApmSpanId
These two properties are printed to the Console using the outputTemplate parameter, of course they can be used with any sink, you could consider using a filesystem sink and Elastic Filebeat for durable and reliable ingestion. This enricher is also compatible with the Elastic.CommonSchema.Serilog package.
The prerequisite for this to work is a configured Elastic APM Agent. If the agent is not configured the enricher won’t add anything to the logs.