Serilog

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

Installation

Using package manager:

Install-Package Sentry.Serilog -Version 1.1.0

Or using the .NET Core CLI:

dotnet add Sentry.Serilog -v 1.1.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.

NOTE: Messages logged from assemblies with the name starting with Sentry will not generate events.

Features

  • 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.

Configuration

You can configure the Sentry Serilog sink as follows:

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:

Log.Logger = new LoggerConfiguration()
  .WriteTo.Sentry(o => o.Dsn = new Dsn("___PUBLIC_KEY___"))
  .CreateLogger();

NOTE: 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.

Options

MinimumBreadcrumbLevel

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

MinimumEventLevel

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

InitializeSdk

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

Samples