Nito.Logging.ExceptionLoggingScope 2.0.0

Prefix Reserved
dotnet add package Nito.Logging.ExceptionLoggingScope --version 2.0.0                
NuGet\Install-Package Nito.Logging.ExceptionLoggingScope -Version 2.0.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="Nito.Logging.ExceptionLoggingScope" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nito.Logging.ExceptionLoggingScope --version 2.0.0                
#r "nuget: Nito.Logging.ExceptionLoggingScope, 2.0.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 Nito.Logging.ExceptionLoggingScope as a Cake Addin
#addin nuget:?package=Nito.Logging.ExceptionLoggingScope&version=2.0.0

// Install Nito.Logging.ExceptionLoggingScope as a Cake Tool
#tool nuget:?package=Nito.Logging.ExceptionLoggingScope&version=2.0.0                

Logo

Nito.Logging Build status codecov NuGet version API docs

A library for using scopes with Microsoft.Extensions.Logging.

What It Does

This library has two parts:

  1. DataScopes provides the BeginDataScope extension methods for ILogger, which allow you to attach name/value pairs of metadata onto your log messages.
  2. ExceptionLoggingScope captures logging scopes when an exception is thrown, and applies those logging scopes when the exception is logged. In other words, if you find your exception logs are missing useful information from your logging contexts, install this library and enjoy rich logs again!

Getting Started

First, install the Nito.Logging package.

To attach data scopes to your logs, call BeginDataScope on any ILogger or ILogger<T>. You can pass an anonymous object, any number of (string, object) tuples, or a collection of KeyValuePair<string, object> (such as a Dictionary<string, object>). See the unit tests for examples.

To preserve logging scopes for exceptions, add a call to AddExceptionLoggingScopes() in your service registration after all loggers have been configured.

Your service registration will look something like this:

public void ConfigureServices(IServiceCollection services)
{
    /* Configure all loggers here */
    ...
    services.AddExceptionLoggingScopes();
    ...
}

Your exception logging will look something like this:

// This example uses custom middleware.
// It's also possible to retrieve and log the exception from an error controller if using the standard exception handling middleware.

public class ExceptionLoggingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<ExceptionLoggingMiddleware> _logger;

    public ExceptionLoggingMiddleware(RequestDelegate next, ILogger<ExceptionLoggingMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next(context);
        }
        catch (Exception ex)
        {
            _logger.Log(ex, "An error occurred.");
            throw;
        }
    }
}

// Don't forget to register the middleware (early in the pipeline):
app.UseMiddleware<ExceptionLoggingMiddleware>();

Alternatives

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 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 is compatible.  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 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on Nito.Logging.ExceptionLoggingScope:

Package Downloads
Nito.Logging

Library for using scopes with Microsoft.Extensions.Logging.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 221 10/1/2024
1.0.1 3,605 2/19/2021
1.0.0 692 10/25/2020
1.0.0-pre06 474 10/25/2020
1.0.0-pre05 402 10/24/2020
1.0.0-pre04 395 9/13/2020
1.0.0-pre03 523 9/12/2020
1.0.0-pre02 380 9/12/2020