Giter VIP home page Giter VIP logo

logging-2's Introduction

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:

  1. Add a call to AddExceptionLoggingScopes() in your service registration.
  2. Call ILogger.BeginCapturedExceptionLoggingScopes(Exception) before logging the exception.

Your service registration will look something like this:

public void ConfigureServices(IServiceCollection services)
{
    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)
        {
            using (_logger.BeginCapturedExceptionLoggingScopes(ex))
                _logger.Log(ex, "An error occurred.");
            throw;
        }
    }
}

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

Alternatives

logging-2's People

Contributors

stephencleary avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.