Serilog enricher for Correlate.
Adds the CorrelationId property by populating the value from ICorrelationContextAccessor.
Install Serilog.Enrichers.Correlate via the Nuget package manager or dotnet
cli.
dotnet add package Serilog.Enrichers.Correlate
The Correlate framework provides an ambient correlation context scope, that makes it easy to track a Correlation ID passing through (micro)services.
This library specifically provides a enricher for Serilog and adds the CorrelationId property to each log event with the value from correlation context provided by Correlate.
Register in service collection
public void ConfigureServices(IServiceCollection services)
{
services.AddCorrelate()
.AddCorrelationContextEnricher();
}
and configure logger
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog(ConfigureLogger)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
private static void ConfigureLogger(HostBuilderContext context, IServiceProvider serviceProvider, LoggerConfiguration loggerConfiguration)
{
loggerConfiguration
.WriteTo.Console()
.Enrich.WithCorrelate(serviceProvider);
}
}
See Correlate documentation for further integration with ASP.NET Core, IHttpClientFactory
and for other extensions/libraries.
See Serilog documentation for simple .NET logging with fully-structured events