ProblemDetails.Serilog.AspNetCore.Middleware.Connector 6.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ProblemDetails.Serilog.AspNetCore.Middleware.Connector --version 6.1.0                
NuGet\Install-Package ProblemDetails.Serilog.AspNetCore.Middleware.Connector -Version 6.1.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="ProblemDetails.Serilog.AspNetCore.Middleware.Connector" Version="6.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProblemDetails.Serilog.AspNetCore.Middleware.Connector --version 6.1.0                
#r "nuget: ProblemDetails.Serilog.AspNetCore.Middleware.Connector, 6.1.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.
// Install ProblemDetails.Serilog.AspNetCore.Middleware.Connector as a Cake Addin
#addin nuget:?package=ProblemDetails.Serilog.AspNetCore.Middleware.Connector&version=6.1.0

// Install ProblemDetails.Serilog.AspNetCore.Middleware.Connector as a Cake Tool
#tool nuget:?package=ProblemDetails.Serilog.AspNetCore.Middleware.Connector&version=6.1.0                

problemdetails-serilog-connector

What does it do?

This package facilitates use of serilog/serilog-aspnetcore alongside khellang/Middleware, to log exceptions at request completion, with a severity level indicating their nature.

It works well with Serilog's default log level computation, which treats http status codes ≥500 as errors🔥, and lower status codes (when an exception is not flying, i.e. it has been caught by Problem Details) as informational 🛈 messages.

nuget Package

ProblemDetails.Serilog.AspNetCore.Middleware.Connector

Usage

Simple

  1. When configuring the application's request pipeline, call UseSerilogRequestLoggingAndProblemDetails instead of UseSerilogRequestLogging and UseProblemDetails.
  2. When configuring the application's services, call AddProblemDetailsAlongsideSerilog instead of AddProblemDetails.

Explicit Middleware Registration

Calling UseSerilogRequestLoggingAndProblemDetails registers three middlewares. These middlewares can be registered explicitly instead:

public void Configure(IApplicationBuilder app)
{
    // ...
    app.UseSerilogRequestLogging(configureOptions); // provided by Serilog
    app.UseProblemDetails(); // provided by Hellang
    app.UseSerilogRequestLoggingCaptureException(); // provided by this package
    // ...
}

Optionally, other middlewares can be mixed in:

public void Configure(IApplicationBuilder app)
{
    // ...
    app.UseSerilogRequestLogging(configureOptions);
    app.UseCoolMiddleware(); // for example
    app.UseProblemDetails();
    app.UseSentryTracing(); // for example
    app.UseSerilogRequestLoggingCaptureException();
    // ...
}

Referenced Components

Serilog and its Diagnostic Context

Serilog provides a convenient diagnostic context capable of capturing an exception that occured at some point during a request. The RequestLoggingMiddleware installed by UseSerilogRequestLogging logs the exception captured in the context when there is no unhandled exception flying.

Problem Details

ProblemDetails catches exceptions and translates them to http responses generally compliant with RFC7807.

Connector Internals

Exception Capturing Middleware

This package provides exception capturing middleware that can be registered using a call to the UseSerilogRequestLoggingCaptureException extension method. This very simple middleware writes any exceptions encountered to Serilog's diagnostic context, without catching them.

Serilog alongside Problem Details

This package provides a UseSerilogRequestLoggingAndProblemDetails extension method that can be used in place of Serilog's UseSerilogRequestLogging and Hellang's UseProblemDetails. It also registers the exception capturing middleware provided by this package, via a call to UseSerilogRequestLoggingCaptureException. The middleware registration order is:

  1. UseSerilogRequestLogging
  2. UseProblemDetails
  3. UseSerilogRequestLoggingCaptureException

The resulting behavior is that exceptions thrown from requests (ex. from MVC controller action methods) are writen to the diagnostic context, then caught by Problem Details, then logged by Serilog.

Problem Details configuration

This package provides a AddProblemDetailsAlongsideSerilog extension method that can be used in place of Hellang's AddProblemDetails. It calls Hellang's AddProblemDetails, but with options defaulted to request that Problem Details doesn't consider as "unhandled", and therefore doesn't log, any exceptions. The options used look like this:

options.ShouldLogUnhandledException = (httpContext, exception, problemDetails) => false;

Alternatives

Some alternatives to using this package are described below.

  • Problem Details can be configured to log additional exceptions using its ShouldLogUnhandledException option, by returning true from the configured function. This approach logs only at the error level, with a message describing logged exceptions as unhandled.
  • Problem Details can be configured to rethrow exceptions, so that other middlewares can see and log them. Serilog's default log level computation will consider all rethrown exceptions still flying to be errors, regardless of the response status code. GetLevel can be configured to consider response status codes even when an exception is flying.
  • Problem Details can be configured to log exceptions using its ShouldLogUnhandledException option, by introducing side effects (e.g. explicit logging to an ILogger, propagating the exception to an IDiagnosticContext) within the configured function.
Product Compatible and additional computed target framework versions.
.NET 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. 
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
8.0.1 755 3/28/2024
6.1.0 1,139 2/7/2023
6.0.2 267 2/7/2023
6.0.1 262 2/6/2023
6.0.0 264 2/6/2023

Problem Details configuration fix, better README, test in net7.0