Microsoft.Extensions.Logging

Sentry has an integration with Microsoft.Extensions.Logging through the Sentry.Extensions.Logging NuGet package.

Add the Sentry dependency:

Copied
Install-Package Sentry.Extensions.Logging -Version 4.4.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.

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

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:

  • ILoggingBuilder
  • ILoggerFactory

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()

Copied
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:

Copied
{
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "Sentry": {
    "Dsn": "https://examplePublicKey@o0.ingest.sentry.io/0",
    "MinimumBreadcrumbLevel": "Debug",
    "MinimumEventLevel": "Warning",
    "MaxBreadcrumbs": 50
  }
}

Above we set the application wide minimum log level to Debug. Sentry specific settings are set under the Sentry property. Options are documented below.

Copied
using (var loggerFactory = new LoggerFactory()
            .AddConsole(LogLevel.Trace)
            .AddSentry(o => o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"))
{
    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.

For example:

Copied
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 any log message. 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.

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.

Copied
options.TagFilters.Add("PROC_");

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").