Serilog + AspNetCore

You can use Waystone.WideLogEvents with Serilog in ASP.NET Core applications by installing the Serilog.Enrichers.Waystone.WideLogEvents.AspNetCore package.

dotnet add package Serilog.Enrichers.Waystone.WideLogEvents.AspNetCore

Usage

Configure Serilog

In your Program.cs, configure Serilog to use the wide log events enricher and the sampling filter:

using Serilog;
using Serilog.Enrichers.Waystone.WideLogEvents;

builder.Host.UseSerilog((context, config) => config
   .Enrich.FromWideLogEventsContext()
   .Filter.WithWideLogEventsSampling()
   .ReadFrom.Configuration(context.Configuration));

Register Middleware

Add the UseWideLogEventsContext middleware to your application pipeline. This middleware should typically be placed early in the pipeline to capture as much information as possible.

using Serilog.Enrichers.Waystone.WideLogEvents.AspNetCore;

var app = builder.Build();

app.UseWideLogEventsContext();

// ... other middleware

Push Properties

You can now push properties to the WideLogEventContext anywhere in your request lifecycle (e.g. in controllers, minimal API handlers, or services):

Configuration Options

You can customize the sampling behavior of the log events by configuring the sampling filter

Random Provider

See Customizing Randomness

Last updated

Was this helpful?