Sentry for 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
  • Send structured logs 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 Log.Error call will create an Event which will include breadcrumbs for prior log messages of level Information, Warning, Error, and Fatal.

Additionally, when enabled, log messages are sent to Sentry as Structured Logs. You can control which logs are captured using the Serilog MinimumLogLevel.

Add the Sentry dependency:

Copied
Install-Package Sentry.Serilog -Version 7.0.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.

If another integration already initializes Sentry, configure the Serilog sink without reinitializing the SDK. Enable EnableLogs on the options used by that integration to send structured logs.

Copied
using Serilog;
using Serilog.Events;

Log.Logger = new LoggerConfiguration()
  .MinimumLevel.Debug()
  .WriteTo.Sentry(o =>
  {
    o.InitializeSdk = false;
    // 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();

When Serilog owns initialization, provide the DSN and enable structured logs in the sink configuration:

Copied
Log.Logger = new LoggerConfiguration()
  .WriteTo.Sentry(o =>
  {
    o.Dsn = "___PUBLIC_DSN___";
    o.EnableLogs = true;
  })
  .CreateLogger();

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.

MinimumBreadcrumbLevel and MinimumEventLevel determine what the sink does with a log event, but every log event the logger captures still reaches the sink. If your logger's minimum level is verbose — for example, because another sink writes debug logs to a file — you can use Serilog's per-sink minimum level to stop lower-level log events from reaching Sentry entirely.

By default this value is Verbose, meaning no log events are filtered out. Ignored when LevelSwitch is set.

An optional LoggingLevelSwitch that allows the sink's minimum level to be changed at runtime.

After configuring Serilog, send an informational log and an error:

Copied
Log.Information("Sentry logging verification");
Log.Error("Sentry error-event verification");

With EnableLogs = true, the informational message appears in Logs. The error message also creates an event in Issues. Call Log.CloseAndFlush() when your application exits to dispose the sink and flush pending data.

  • Explore practical guides on what to monitor, log, track, and investigate after setup
Was this helpful?
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").