Skip to content

PaulUpson/serilog-enrichers-http-header

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serilog.Enrichers.HttpHeader

Enriches Serilog events with a custom http header for tracking requests.

NuGet

To use the enricher, first install the NuGet package:

Install-Package Serilog.Enrichers.HttpHeader

Then, apply the enricher to your LoggerConfiguration:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithHttpHeader("x-correlation-id", "CorrelationId")
    // ...other configuration...
    .CreateLogger();

The WithHttpHeader(headerKey, propertyName) enricher allows you to specify a headerKey to retrieve from the http request and the name of the property to use when logging.

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.WithHttpHeader("x-correlation-id", "CorrelationId")
                .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.

About

Enrich logs with an http header so you can track logs for specific requests.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%