Skip to content

Latest commit

 

History

History
97 lines (75 loc) · 3.54 KB

File metadata and controls

97 lines (75 loc) · 3.54 KB

Serilog.Enrichers.CorrelationId

Enriches Serilog events with a correlation ID for tracking requests.

Build status Coverage Status NuGet

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

The WithCorrelationId() enricher will add a CorrelationId property to produced events.

Included enrichers

The package includes:

  • WithCorrelationId() - adds a CorrelationId to track logs for the current web request.
  • WithCorrelationIdHeader(headerKey) - adds a CorrelationId extracted from the current request header (or created if one does not exist).

Installing into an ASP.NET Core Web Application

This is what your Startup class should contain in order for this enricher to work as expected:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace MyWebApp
{
    public class Startup
    {
        public Startup()
        {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                .Enrich.WithCorrelationId()
                .CreateLogger();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // ...
            services.AddHttpContextAccessor();
            // ...
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // ...
            loggerFactory.AddSerilog();
            // ...
        }
    }
}

You need to register the IHttpContextAccessor singleton so that the enricher has access to the requests HttpContext so that it can attach the correlation ID to the request/response.

ASP.NET Core - CorrelationIdHeaderSupplier Middleware

Additionally you can use CorrelationIdHeaderSupplierMiddleware in your application by calling UseCorrelationIdHeaderSupplier(headerKey). It is responsible for:

  • rewrite x-correlation-id header from Request to Response
  • create x-correlation-id header and supply Response when it is not provided in the request
using Serilog.Enrichers.CorrelationId.Middlewares;

public void ConfigureServices(IServiceCollection services)
{
    // ...
    app.UseCorrelationIdHeaderSupplier();
    app.UseMvc();
    // ...
}