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.
- Host: GitHub
- URL: https://github.com/optical/serilogexceptionallogcontext
- Owner: optical
- License: mit
- Created: 2020-08-05T09:10:54.000Z (almost 6 years ago)
- Default Branch: develop
- Last Pushed: 2022-11-29T09:53:08.000Z (over 3 years ago)
- Last Synced: 2025-04-10T21:47:49.422Z (over 1 year ago)
- Topics: serilog, serilog-enricher, serilog-enrichers
- Language: C#
- Homepage:
- Size: 18.6 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serilog.ExceptionalLogContext
[](https://nuget.org/packages/Serilog.ExceptionalLogContext) 
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)