Serilog.Sinks.Router 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Serilog.Sinks.Router --version 1.2.0
                    
NuGet\Install-Package Serilog.Sinks.Router -Version 1.2.0
                    
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.Router" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Sinks.Router" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Serilog.Sinks.Router" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Serilog.Sinks.Router --version 1.2.0
                    
#r "nuget: Serilog.Sinks.Router, 1.2.0"
                    
#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.
#:package Serilog.Sinks.Router@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Serilog.Sinks.Router&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Serilog.Sinks.Router&version=1.2.0
                    
Install as a Cake Tool

Router Sink for Serilog

Uses DynamicExpresso and IOptions tracking to enable dynamic routing of LogEvents between two different sinks.

NuGet version (Serilog.Sinks.Router)

Targets

Usage

From inside a ASP.Net Core application:

var builder = WebApplication.CreateBuilder(args);

// To enable dynamic recompilation you must enable the "reloadOnChange" flag.
builder.Configuration.AddJsonFile("logging.json", optional: false, reloadOnChange: true);

builder.Services.AddOptions();
builder.Services.Configure<RouterSinkOptions>(builder.Configuration.GetSection("RouterSink"));

// This example uses the ApplicationInsights sink as the A sink.
builder.Services.AddApplicationInsightsTelemetry();

builder.Host.UseSerilog((context, services, configuration) =>
{
    configuration
        .ReadFrom.Configuration(context.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext()
        .WriteTo.RouterSink(services.GetRequiredService<IOptionsMonitor<RouterSinkOptions>>(), sinkAConf => sinkAConf.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(),
            TelemetryConverter.Traces), sinkBConf => sinkBConf.Console());
});
Configuration

The current ILogEvent is made implicitly available by the configured DynamicExpresso interpreter. So accessing the Properties of the ILogEvent is as simple as using Properties[keyForPropertyYouWant].

Simple example based on event level:

This configuration only uses the A sink if the log event level is debug, and everything else will get logged by the B sink.

{
    "RouterSink": {
        "ShouldEmitSinkAExpression": "Level == LogEventLevel.Debug",
        "ShouldEmitSinkBExpression": ""
    }
}
More complex example based on request host information:

Now the A sink only logs if the port number from the request host information contains the port 7443. The B sink is currently disabled, but that can be changed dynamically at runtime without restarting the server.

{
    "RouterSink": {
        "ShouldEmitSinkAExpression": "Properties.Keys.Contains(\"RequestPath\") && Properties.Keys.Contains(\"Host\") && Properties[\"Host\"].ToString().Contains(\":7443\")",
        "ShouldEmitSinkBExpression": "false"
    }
}

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.3.0 658 6/13/2023
1.2.0 228 6/13/2023
1.1.0 238 6/12/2023
1.0.0 235 6/12/2023

Initial release.