Serilog.Sinks.Intercepter 1.0.2

dotnet add package Serilog.Sinks.Intercepter --version 1.0.2
NuGet\Install-Package Serilog.Sinks.Intercepter -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.Intercepter" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.Intercepter --version 1.0.2
#r "nuget: Serilog.Sinks.Intercepter, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Sinks.Intercepter as a Cake Addin
#addin nuget:?package=Serilog.Sinks.Intercepter&version=1.0.2

// Install Serilog.Sinks.Intercepter as a Cake Tool
#tool nuget:?package=Serilog.Sinks.Intercepter&version=1.0.2

Serilog.Sinks.Intercepter

NuGet GitHub action build License Coverage Status

Modify, filter, buffer event logs. Buffer log messages and conditionally output them based on later events.

A wrapper for other Serilog sinks. This sink allows you to "intercept" log events just as they are written to a wrapped sink. This is used to modify, filter, buffer event logs. This is especially suited to reducing log volume, for example only writing logs when an error has occurred.

Getting started

Install from NuGet:

Install-Package Serilog.Sinks.Intercepter

Assuming you have already installed the target sink, such as the console sink, move the wrapped sink's configuration within a WriteTo.Intercepter() statement:

Log.Logger = new LoggerConfiguration()
  .WriteTo.Intercepter(x => x.Console())
  // Other logger configuration
  .CreateLogger()

Log.Information("Continue to use the log as normal");

// At application shutdown
Log.CloseAndFlush();

Whilst no Intercepter has been set, all log messages will be sent onwards to the wrapped sink (Console in this case).

LogLevelBuffer Intercepter

This Intercepter is designed to reduce log volume by storing log events and only writing when an error level log event is received.

// add the Intercepter
using (IntercepterContext.PushLogLevelBuffer())
{
    Log.Information("This log is stored by the intercepter.");
    Log.Error("On this error, this and all previous log events are sent to the wrapped sink");
    Log.Information("As there has already been an error, this is sent to");
}

Can be used per application request to only write the logs of requests where there was an error.

ASP.NET Core integration

See samples/Sample.WebApp/ for the full sample of using this with ASP.NET Core.

The configuration used in the sample is similar to the first we saw:

builder.Host.UseSerilog((ctx, lc) =>
  lc.WriteTo.Intercepter(x => x.Console()));

The sample shows how to add IntercepterContext.PushLogLevelBuffer to the ASP.NET Core pipeline as middleware:

public async Task Invoke(HttpContext httpContext)
{
  using (IntercepterContext.PushLogLevelBuffer())
  {
      await _next(httpContext);
  }
}

JSON configuration

Using Serilog.Settings.Configuration JSON:

{
  "Serilog": {
    "WriteTo": [{
    "Name": "Intercepter",
    "Args": {
      "configure": [{
        "Name": "Console"
        }]
      }
    }]
  }
}

Log event ordering

In the event of any buffering, the log events sent to the output are in the original order and with the original timestamps.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 242 8/21/2023
1.0.1 125 8/18/2023
1.0.0 176 2/5/2023

Adding greater support for older versions of Serilog