Microsoft.Extensions.Logging
Learn about Sentry's .NET integration with Microsoft.Extensions.Logging.
Sentry has an integration with Microsoft.Extensions.Logging through the Sentry.Extensions.Logging 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 LogError call will create an Event which will include breadcrumbs for all prior log messages of level Information, Warning, Error and Critical.
Additionally, when enabled, log messages are sent to Sentry as Structured Logs. Filtering to control the minimum log level to capture should be done via Microsoft.Extensions.Logging configuration.
Add the Sentry dependency:
Install-Package Sentry.Extensions.Logging -Version 6.6.0
Install-Package Sentry.Extensions.Logging -Version 6.6.0
dotnet add package Sentry.Extensions.Logging -v 6.6.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.
Messages logged from assemblies with the name starting with Sentry will not generate events.
There are two ways to add this integration into your project. In both cases it means calling an extension method. The places where it's available are:
ILoggingBuilderILoggerFactory
The ILoggingBuilder extension is useful when using a WebHostBuilder or a HostBuilder, also known as Generic host. This extension method is used by the ASP.NET Core integration under the hood to automatically.
The extension to ILoggerFactory is a convenient way when only the LoggerFactory is being used, without any framework integration.
In both cases the method call is:
AddSentry()
public static Task Main()
=> new HostBuilder()
.ConfigureHostConfiguration(c =>
{
c.SetBasePath(Directory.GetCurrentDirectory());
c.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureLogging((c, l) =>
{
l.AddConfiguration(c.Configuration);
// Adding Sentry integration to Microsoft.Extensions.Logging
l.AddSentry();
})
.Build();
public static Task Main()
=> new HostBuilder()
.ConfigureHostConfiguration(c =>
{
c.SetBasePath(Directory.GetCurrentDirectory());
c.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureLogging((c, l) =>
{
l.AddConfiguration(c.Configuration);
// Adding Sentry integration to Microsoft.Extensions.Logging
l.AddSentry();
})
.Build();
In this example, a generic host is being built and the MEL integration with Sentry added through ConfigureLogging. The example also demonstrates how to bind the appsettings.json configuration to make the values available to the Sentry integration.
With this approach, options like Dsn, SendDefaultPii, MinimumBreadcrumbLevel, to name a few, can be set in appsettings.json.
Example using appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning"
}
},
"Sentry": {
"Dsn": "___PUBLIC_DSN___",
"SendDefaultPii": true,
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"MaxBreadcrumbs": 50
}
}
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning"
}
},
"Sentry": {
"Dsn": "___PUBLIC_DSN___",
"SendDefaultPii": true,
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"MaxBreadcrumbs": 50
}
}
Above we set the application wide minimum log level to Debug and we configure a minimum log level for Microsoft specific logs to Warning. Sentry specific settings are set under the Sentry property. Options are documented below.
using (var loggerFactory = new LoggerFactory()
.AddConsole(LogLevel.Trace)
.AddSentry(o => o.Dsn = "___PUBLIC_DSN___"))
{
var logger = loggerFactory.CreateLogger<Program>();
}
using (var loggerFactory = new LoggerFactory()
.AddConsole(LogLevel.Trace)
.AddSentry(o => o.Dsn = "___PUBLIC_DSN___"))
{
var logger = loggerFactory.CreateLogger<Program>();
}
This approach doesn't include any of the framework's configuration system. Since the DSN is being provided via parameter, the SDK will be initialized.
More settings can be passed via the callback configuring the SentryOptions.
In cases where AddSentry is called without arguments, it's worth noting that an overload exists where SentryOptions can be configured just like if SentrySdk.Init had been called.
The SDK needs to be initialized only once. If the 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 configuration, set the InitializeSdk flag to false. Not providing a DSN or leaving it as an empty string will disable the SDK.
For example:
using (var loggerFactory = new LoggerFactory()
.AddSentry(o => o.InitializeSdk = false))
{ }
using (var loggerFactory = new LoggerFactory()
.AddSentry(o => o.InitializeSdk = false))
{ }
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.
A list of filters which are invoked before processing log messages. This allows you to inspect the details of the log entry before they become a Breadcrumb or an Event with full access to the Microsoft.Extensions.Logging data.
Note
These filters only apply to breadcrumbs and events. They don't control which logs will be captured as structured logs in Sentry. That can be controlled independently using the filtering capabilities built into Microsoft.Extensions.Logging.
A list of filters which are invoked before processing any log message to filter out undesired tags.
For example, to remove all tags starting with PROC_, add a tag filter to your SentryOptions when initializing the SDK.
options.TagFilters.Add("PROC_");
options.TagFilters.Add("PROC_");
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
try
{
throw null;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
try
{
throw null;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
try
raise <| NullReferenceException ()
with ex ->
SentrySdk.CaptureException(ex) |> ignore
- A simple example using simply the
LoggerFactory. - An example using the generic host.
- 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").