Skip to content

serilog-contrib/serilog-sinks-sentry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serilog.Sinks.Sentry

Note

This is an unofficial library based on the old Raven SDK, the only use case for this is legacy projects (.NET Framework 4.5 to 4.6.0)
For .NET Framework 4.6.1, .NET Core 2.0, Mono 5.4 or higher, please use the new SDK and the Serilog sink.

Build status Quality Gate

Package
Serilog.Sinks.Sentry NuGet
Serilog.Sinks.Sentry.AspNetCore NuGet

A Sentry sink for Serilog.

Installation

The library is available as a Nuget package.

Install-Package Serilog.Sinks.Sentry

Demos

You can find demo .NET Core apps here.

Get started

Adding Sentry sink

var log = new LoggerConfiguration()
    .WriteTo.Sentry("Sentry DSN")
    .Enrich.FromLogContext()
    .CreateLogger();

// By default, only messages with level errors and higher are captured
log.Error("This error goes to Sentry.");

Data scrubbing

Some of the logged content might contain sensitive data and should therefore not be sent to Sentry. When setting up the Sentry Sink it is possible to provide a custom IScrubber implementation which will be passed the serialized data that is about to be sent to Sentry for scrubbing / cleaning.

Adding a scrubber would look like this:

var log = new LoggerConfiguration()
    .WriteTo.Sentry("Sentry DSN", dataScrubber: new MyDataScrubber())
    .Enrich.FromLogContext()
    .CreateLogger();

MyDataScrubber has to implement the interface SharpRaven.Logging.IScrubber. Check the Web Demo Startup.cs for further details and the example implementation of a scrubber .

Capturing HttpContext (ASP.NET Core)

In order to capture a user, request body and headers, some additional steps are required.

Install the additional sink for ASP.NET Core

Install-Package Serilog.Sinks.Sentry.AspNetCore

Specify custom HttpContext destructing policy

var log = new LoggerConfiguration()
    .WriteTo.Sentry("Sentry DSN")
    .Enrich.FromLogContext()

    // Add this two lines to the logger configuration
    .Destructure.With<HttpContextDestructingPolicy>()
    .Filter.ByExcluding(e => e.Exception?.CheckIfCaptured() == true)

    .CreateLogger();

Add Sentry context middleware in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // Add this line
    app.AddSentryContext();

    // Other stuff
}