Serilog support for ASP.NET Core logging alongside Problem Details
$ dotnet add package ProblemDetails.Serilog.AspNetCore.Middleware.ConnectorThis 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.
ProblemDetails.Serilog.AspNetCore.Middleware.Connector
UseSerilogRequestLoggingAndProblemDetails instead of UseSerilogRequestLogging and UseProblemDetails.AddProblemDetailsAlongsideSerilog instead of AddProblemDetails.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();
// ...
}
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.
ProblemDetails catches exceptions and translates them to http responses generally compliant with RFC7807.
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.
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:
UseSerilogRequestLoggingUseProblemDetailsUseSerilogRequestLoggingCaptureExceptionThe 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.
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;Some alternatives to using this package are described below.
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.GetLevel can be configured to consider response status codes even when an exception is flying.ShouldLogUnhandledException option, by introducing side effects (e.g. explicit logging to an ILogger, propagating the exception to an IDiagnosticContext) within the configured function.