An open API service indexing awesome lists of open source software.

https://github.com/optical/serilogexceptionallogcontext

A Serilog enricher which captures exception log context as soon as the exception is raised, preventing loss of context.
https://github.com/optical/serilogexceptionallogcontext

serilog serilog-enricher serilog-enrichers

Last synced: over 1 year ago
JSON representation

A Serilog enricher which captures exception log context as soon as the exception is raised, preventing loss of context.

Awesome Lists containing this project

README

          

# Serilog.ExceptionalLogContext
[![Nuget](https://img.shields.io/nuget/v/Serilog.ExceptionalLogContext.svg)](https://nuget.org/packages/Serilog.ExceptionalLogContext) ![Compile](https://github.com/optical/SerilogExceptionalLogContext/workflows/.NET%20Core%20Compile/badge.svg)

An enricher for Serilog which will enhance your exceptions logged with their LogContext at the time they were thrown. If you use LogContext to enrich your logs with additional ambient details, but are frustrated that your low level exception logs lack this context, this enricher is for you.

This solves the issue described in [serilog/#984](https://github.com/serilog/serilog/issues/895). There is no need to use an exception filter and it also works for async/await too, all without changing any of your existing exception handling code

The problem described in code:
```
try {
LogContext.Push("InstanceId", Guid.NewGuid()) {
await RunMyapplication();
}
} catch (Exception exception) {
// Without Serilog.ExceptionalLogContext this log line won't have any of our LogContext properties as the stack has been unwound already, disposing of them :(
Log.Error(exception, "Oh no, something bad happened");
}
```

With Serilog.ExceptionalLogContext your log LogContext state is cloned immediately when an exception is thrown, so that when your application eventually gets around to logging it, you'll still get to keep all that key information.

## Usage
It's as simple as adding `.Enrich.WithExceptionalLogContext()` and you're done!

```
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionalLogContext()
// Write to a sink, add more enrichers, etc
.CreateLogger();
```

## Acquistion
Download from [Nuget](https://nuget.org/packages/Serilog.ExceptionalLogContext)