/serilog-enrichers-correlate

Serilog enricher for Correlate. Adds the CorrelationId property by populating the value from ICorrelationContextAccessor.

Primary LanguageC#MIT LicenseMIT

Serilog.Enrichers.Correlate

Serilog enricher for Correlate.

Adds the CorrelationId property by populating the value from ICorrelationContextAccessor.

Installation

Install Serilog.Enrichers.Correlate via the Nuget package manager or dotnet cli.

dotnet add package Serilog.Enrichers.Correlate

Nuget Nuget

Correlation ID flow

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.

Usage

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);
    }
}

More info

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