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:
Install-Package Sentry.Serilog -Version 7.0.0
Install-Package Sentry.Serilog -Version 7.0.0
dotnet add package Sentry.Serilog -v 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.
Note
The integration excludes the Sentry SDK's own logger categories to avoid capturing its diagnostic messages as events. Application categories that happen to start with Sentry aren't excluded solely because of that prefix.
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.
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();
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();
{
"Serilog": {
"Using": ["Sentry.Serilog"],
"MinimumLevel": {
"Default": "Debug"
},
"WriteTo": [
{
"Name": "Sentry",
"Args": {
"minimumBreadcrumbLevel": "Debug",
"minimumEventLevel": "Warning"
}
}
]
}
}
When Serilog owns initialization, provide the DSN and enable structured logs in the sink configuration:
Log.Logger = new LoggerConfiguration()
.WriteTo.Sentry(o =>
{
o.Dsn = "___PUBLIC_DSN___";
o.EnableLogs = true;
})
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Sentry(o =>
{
o.Dsn = "___PUBLIC_DSN___";
o.EnableLogs = true;
})
.CreateLogger();
{
"Serilog": {
"Using": ["Sentry.Serilog"],
"WriteTo": [
{
"Name": "Sentry",
"Args": {
"dsn": "___PUBLIC_DSN___",
"enableLogs": true
}
}
]
}
}
Initialize the SDK once. The C# callback overload defaults InitializeSdk to true and can resolve a DSN from SENTRY_DSN. Set InitializeSdk = false when another integration or SentrySdk.Init owns initialization. Omitting the DSN from that callback doesn't prevent reinitialization: it uses SENTRY_DSN if set and replaces the existing SDK configuration, and otherwise throws because no DSN was found.
The configuration-file overload infers initialization from its dsn argument. In the first JSON example, Sentry must already be initialized elsewhere; supplying dsn, as in the second example, enables initialization through the sink.
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.
Note
This also controls which log entries are sent to Sentry as structured logs
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:
Log.Information("Sentry logging verification");
Log.Error("Sentry error-event verification");
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
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").