Serilog

Learn about Sentry's .NET integration with Serilog.

Sentry provides an integration with Serilog through the Sentry.Serilog NuGet package.

  • Store log messages as breadcrumbs
  • Send events to sentry

Two separate settings define the minimum log level to keep the log entry as a Breadcrumb and to send an Event to Sentry. The events include any stored breadcrumb on that scope.

By default, any message with log level Information or higher will be kept as a Breadcrumb.

The default value to report a log entry as an event to Sentry is Error.

This means that out of the box, any LogError call will create an Event which will include all log messages of level Information, Warning and also Error and Critical.

Add the Sentry dependency:

Copied
Install-Package Sentry.Serilog -Version 4.13.0

This package extends Sentry main SDK. That means that besides the logging related features, through this package you'll also get access to all API and features available in the main Sentry SDK.

You can configure the Sentry Serilog sink as follows:

Copied
Log.Logger = new LoggerConfiguration()
  .WriteTo.Sentry(o =>
  {
    // Debug and higher are stored as breadcrumbs (default is Information)
    o.MinimumBreadcrumbLevel = LogEventLevel.Debug;
    // Warning and higher is sent as event (default is Error)
    o.MinimumEventLevel = LogEventLevel.Warning;
  })
  .CreateLogger();

It's also possible to initialize the SDK through the Serilog integration. This is useful when the Serilog is the only integration being used in your application. To initialize the Sentry SDK through the Serilog integration, provide it with the DSN:

Copied
Log.Logger = new LoggerConfiguration()
  .WriteTo.Sentry(o => o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0")
  .CreateLogger();

The SDK only needs to be initialized once. If a DSN is made available to this integration, by default it will initialize the SDK. If you do not wish to initialize the SDK via this integration, set the InitializeSdk flag to false. Not providing a DSN or leaving it as null instructs the integration not to initialize the SDK and unless another integration initializes it or you call SentrySdk.Init, the SDK will stay disabled.

A LogLevel which indicates the minimum level a log message has to be included as a breadcrumb. By default this value is Information.

A LogLevel which indicates the minimum level a log message has to be sent to Sentry as an event. By default this value is Error.

Whether or not this integration should initialize the SDK. If you intend to call SentrySdk.Init yourself you should set this flag to false.

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").