Enriches Serilog events with a correlation ID for tracking requests.
To use the enricher, first install the NuGet package:
Install-Package Serilog.Enrichers.CorrelationId
Then, apply the enricher to your LoggerConfiguration
:
Log.Logger = new LoggerConfiguration()
.Enrich.WithCorrelationId()
// ...other configuration...
.CreateLogger();
IT IS CRUCIAL FOR THIS ENRICHER TO WORK TO ENABLE ACCESS TO HTTP CONTEXT
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
..
services.AddHttpContextAccessor();
..
}
The WithCorrelationId()
enricher will add a CorrelationId
property to produced events.
The package includes:
WithCorrelationId()
- adds aCorrelationId
to track logs for the current web request.WithCorrelationIdHeader(headerKey)
- adds aCorrelationId
extracted from the current request header (or created if one does not exist).