Learn about Sentry's .NET integration with NLog.

Sentry provides an integration with NLog through the Sentry.NLog NuGet package.

  • 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, Sentry will keep any message with log level Info or higher 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 Error call will create an Event which will include all log messages of level Info, Warn and also Error and Critical.

Add the Sentry dependency:

Install-Package Sentry.NLog -Version 4.7.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.

You can configure the Sentry NLog target via code as follows:

LogManager.Configuration = new LoggingConfiguration();
    .AddSentry(options =>
        // Optionally specify a separate format for message
        options.Layout = "${message}";
        // Optionally specify a separate format for breadcrumbs
        options.BreadcrumbLayout = "${logger}: ${message}";

        // Debug and higher are stored as breadcrumbs (default is Info)
        options.MinimumBreadcrumbLevel = LogLevel.Debug;
        // Error and higher is sent as event (default is Error)
        options.MinimumEventLevel = LogLevel.Error;

        // Send the logger name as a tag
        options.AddTag("logger", "${logger}");

        // All Sentry Options are accessible here.

It's also possible to initialize the SDK through the NLog integration (as opposed to using SentrySdk.Init). This is useful when NLog is the only integration being used in your application. To initialize the Sentry SDK through the NLog integration, provide it with the DSN:

LogManager.Configuration = new LoggingConfiguration();
    .AddSentry(o =>
        // The NLog integration will initialize the SDK if DSN is set:
        o.Dsn = "";

Two log levels are used to configure this integration (see options below). One will configure the lowest level required for a log message to become an event (MinimumEventLevel) sent to Sentry. The other option (MinimumBreadcrumbLevel) configures the lowest level a message has to be to become a breadcrumb. Breadcrumbs are kept in memory (by default the last 100 records) and are sent with events. For example, by default, if you log 100 entries with logger.Info or logger.Warn, no event is sent to Sentry. If you then log with logger.Error, an event is sent to Sentry which includes those 100 Info or Warn messages. For this to work, SentryTarget needs to receive all log entries in order to decide what to keep as breadcrumb or sent as event. Make sure to set the NLog LogLevel configuration to a value lower than what you set for the MinimumBreadcrumbLevel and MinimumEventLevel to make sure SentryTarget receives these log messages.

The SDK can also be configured via NLog.config XML file:

<?xml version="1.0" encoding="utf-8" ?>
    <add assembly="Sentry.NLog" />


      <!-- Advanced options can be configured here-->
        <!--Advanced options can be specified as attributes or elements-->

      <!--Optionally add any desired additional Tags that will be sent with every message -->

      <!--Optionally add any desired additional Data that will be sent with every message -->

    <logger name="*" writeTo="sentry" />

A LogLevel that indicates the minimum level a log message needs to be in order to become a breadcrumb. By default, this value is Info.

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.

To ignore log messages that don't contain an exception.

Determines whether event-level properties will be sent to Sentry as additional data. Defaults to true.

Determines whether event properties will be sent to Sentry as Tags or not. Defaults to false.

Determines whether or not to include event-level data as data in breadcrumbs for future errors. Defaults to false.

Custom layout for breadcrumbs. See NLog layout renderers for more.

Configured layout for the NLog logger.

Any additional tags to apply to each logged message.

You can provide a user entry matching the following example user. See Identify Users for more information.

 <other name="mood" layout="joyous" />

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