Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ Then configure the Sentry target in an `NLog.config` XML file:
ignoreEventsWithNoException="False"
includeEventDataOnBreadcrumbs="True"
includeEventPropertiesAsTags="True"
minimumEventLevel="Error">
minimumEventLevel="Error"
minimumLogLevel="Info">

<!-- Optionally specify user properties via NLog
(here using MappedDiagnosticsLogicalContext as an example) -->
Expand Down Expand Up @@ -98,6 +99,7 @@ LogManager.Configuration.AddSentry(o =>

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

o.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs

Expand Down Expand Up @@ -128,6 +130,7 @@ LogManager.Configuration.AddSentry(

o.MinimumBreadcrumbLevel <- LogLevel.Debug // Debug and higher are stored as breadcrumbs (default is Info)
o.MinimumEventLevel <- LogLevel.Error // Error and higher is sent as event (default is Error)
o.MinimumLogLevel <- LogLevel.Info // Info and higher are sent as logs (default is Info)

o.IncludeEventDataOnBreadcrumbs <- true // Optionally include event properties with breadcrumbs

Expand Down
14 changes: 10 additions & 4 deletions docs/platforms/dotnet/guides/nlog/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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`, `Error`, and `Fatal` stored as breadcrumbs.

Log messages are also sent to Sentry as [Structured Logs](logs/).
Log messages at `Info` or higher are also sent to Sentry as [Structured Logs](logs/). Set [`MinimumLogLevel`](#minimumloglevel) to change that level.

## Install

Expand Down Expand Up @@ -67,6 +67,8 @@ config.AddSentry(options =>
options.MinimumBreadcrumbLevel = LogLevel.Debug;
// Error and higher is sent as event (default is Error)
options.MinimumEventLevel = LogLevel.Error;
// Info and higher are sent as logs (default is Info)
options.MinimumLogLevel = LogLevel.Info;

// Send the logger name as a tag
options.AddTag("logger", "${logger}");
Expand All @@ -78,7 +80,7 @@ The target drops log events while Sentry isn't initialized. If a DSN can be foun

### Minimum log level

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.
Three log levels configure this integration. See [Options](#options) below. `MinimumEventLevel` is the lowest level for a log message to become an event sent to Sentry. `MinimumBreadcrumbLevel` is the lowest level for a message to become a breadcrumb. `MinimumLogLevel` is the lowest level for a message to be sent as a structured log. 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. `AddSentry` adds a rule that passes the target every entry at the lowest of the three levels or higher. If you write the rule yourself, for example in `NLog.config`, make sure its minimum level isn't higher than `MinimumBreadcrumbLevel`, `MinimumEventLevel` or `MinimumLogLevel`, so `SentryTarget` receives these log messages.

The target can also be configured in an `NLog.config` XML file:

Expand All @@ -98,6 +100,7 @@ The target can also be configured in an `NLog.config` XML file:
breadcrumbLayout="${message}"
minimumBreadcrumbLevel="Debug"
minimumEventLevel="Error"
minimumLogLevel="Info"
includeEventDataOnBreadcrumbs="true">

<!--Optionally add any desired additional Tags that will be sent with every message -->
Expand Down Expand Up @@ -128,15 +131,14 @@ To send these crashes, also call `SentrySdk.Init` before you create the builder,
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
using Sentry.NLog;

using var sentry = SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
});

var config = new LoggingConfiguration();
config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, new SentryTarget());
config.AddSentry();
LogManager.Configuration = config;
var logger = LogManager.GetCurrentClassLogger();

Expand Down Expand Up @@ -180,6 +182,10 @@ A `LogLevel` that indicates the minimum level a log message needs to be in order

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

#### MinimumLogLevel

A `LogLevel` which indicates the minimum level a log message needs to be sent to Sentry as a structured log. By default this value is `Info`. Set it to `Off` to send no logs.

#### IgnoreEventsWithNoException

To ignore log messages that don't contain an exception.
Expand Down
5 changes: 2 additions & 3 deletions includes/dotnet-usesentry-logging/nlog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ In an app that calls `UseSentry`, add NLog with `AddNLog` from the `NLog.Extensi
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
using Sentry.NLog;

var config = new LoggingConfiguration();
config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, new SentryTarget());
config.AddSentry();
LogManager.Configuration = config;

builder.Logging.AddNLog(new NLogProviderOptions { ReplaceLoggerFactory = true });
Expand All @@ -17,4 +16,4 @@ builder.UseSentry(options =>
});
```

`ReplaceLoggerFactory` replaces the app's `ILogger` factory with NLog's, so each `ILogger` entry reaches Sentry once, through the target. Without it, NLog runs alongside the `ILogger` integration that `UseSentry` adds, and Sentry receives every entry twice. NLog's rules then decide which entries are logged, instead of the `Logging` section of your configuration. The target sends every entry it receives to Sentry as a log, so the rule's minimum level is also the minimum level for logs. In ASP.NET Core, `UseNLog` from the `NLog.Web.AspNetCore` package takes the same option.
`ReplaceLoggerFactory` replaces the app's `ILogger` factory with NLog's, so each `ILogger` entry reaches Sentry once, through the target. Without it, NLog runs alongside the `ILogger` integration that `UseSentry` adds, and Sentry receives every entry twice. NLog's rules then decide which entries are logged, instead of the `Logging` section of your configuration. The target sends entries at `Info` or higher to Sentry as logs. To change that level, set [`MinimumLogLevel`](/platforms/dotnet/guides/nlog/#minimumloglevel). In ASP.NET Core, `UseNLog` from the `NLog.Web.AspNetCore` package takes the same option.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ builder.UseSentry(options =>

A generic host app that set `EnableLogs` on `Logging.AddSentry` in 6.x sets it on `UseSentry` now.

The Serilog sink no longer takes an `enableLogs` argument. NLog's `enableLogs` setting no longer turns logs off, and setting it to `false` writes a warning to standard error. To drop logs and keep errors and breadcrumbs, use `SetBeforeSendLog`, as shown in [Logging Integrations Send Logs](#logging-integrations-send-logs).
The Serilog sink no longer takes an `enableLogs` argument. NLog's `enableLogs` setting is obsolete. `enableLogs="false"` still turns the target's logs off, and `enableLogs="true"` has no effect. Set `minimumLogLevel` to `Off` instead. To drop logs from any integration and keep errors and breadcrumbs, use `SetBeforeSendLog`, as shown in [Logging Integrations Send Logs](#logging-integrations-send-logs).
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ platformCategory: all
order: 10
---

The Serilog sink, the NLog target, the log4net appender and `Logging.AddSentry()` after `SentrySdk.Init` send every entry they receive to Sentry as a log. In 6.x, they sent logs only with `EnableLogs` on, so an upgraded app can send many more logs.
The Serilog sink, the NLog target, the log4net appender and `Logging.AddSentry()` after `SentrySdk.Init` send the entries they receive to Sentry as logs. In 6.x, they sent logs only with `EnableLogs` on, so an upgraded app can send many more logs.

Adding one of these integrations is now the opt-in for its logs, in every kind of app. `EnableLogs` on the options of `UseSentry` doesn't change this. See [EnableLogs Is Removed from SentryOptions](#enablelogs-is-removed-from-sentryoptions).

Each integration sends the entries its logging framework passes to it:

- The Serilog sink gets entries at the logger's minimum level, which is `Information` unless you change it.
- The NLog target gets the levels of the rules that write to it.
- The NLog target gets the levels of the rules that write to it, and sends those at `Info` or higher. Set `MinimumLogLevel` on the target to change that level.
- The log4net appender gets entries at its `threshold`.
- `Logging.AddSentry()` gets the levels from the `Logging` configuration, which default to `Information`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ LogManager.Configuration = new LoggingConfiguration()
.AddSentry(options => options.MinimumEventLevel = LogLevel.Error);
```

NLog's `enableLogs` setting is obsolete and has no effect, because the target now sends logs once you add it. See [Logging Integrations Send Logs](#logging-integrations-send-logs). The target now flushes with `SentryOptions.FlushTimeout`, which defaults to 2 seconds instead of 15. See [Logging Frameworks No Longer Close Sentry](#logging-frameworks-no-longer-close-sentry).
The target now sends logs once you add it, so its `enableLogs` setting is obsolete. `enableLogs="false"` still turns logs off, and `enableLogs="true"` has no effect. In 6.x, once logs were on, the target sent every entry it received as a log. In 7.x, it sends entries at `Info` or higher, so set `minimumLogLevel` to keep sending `Trace` or `Debug` entries. See [Logging Integrations Send Logs](#logging-integrations-send-logs). The target now flushes with `SentryOptions.FlushTimeout`, which defaults to 2 seconds instead of 15. See [Logging Frameworks No Longer Close Sentry](#logging-frameworks-no-longer-close-sentry).
16 changes: 16 additions & 0 deletions platform-includes/logs/options/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ For background on provider/category filtering, see [Logging in C# and .NET](http

</PlatformSection>

<PlatformSection supported={["dotnet.nlog"]}>

#### MinimumLogLevel

The NLog target sends entries at `Info` or higher as logs. To also send `Debug` entries, lower `MinimumLogLevel` when you add the target:

```csharp
var config = new LoggingConfiguration();
config.AddSentry(options => options.MinimumLogLevel = NLog.LogLevel.Debug);
LogManager.Configuration = config;
```

In `NLog.config`, set the `minimumLogLevel` attribute on the target instead. Set it to `Off` to send no logs from NLog.

</PlatformSection>

#### SetBeforeSendLog

To filter logs or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func<SentryLog, SentryLog?>)` option.
Expand Down
2 changes: 1 addition & 1 deletion platform-includes/logs/setup/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Log.Logger = new LoggerConfiguration()

<PlatformSection supported={["dotnet.nlog"]}>

Initialize the SDK, then add the Sentry target, in code or in your `NLog.config`. The target sends logs as soon as you add it.
Initialize the SDK, then add the Sentry target, in code or in your `NLog.config`. As soon as you add the target, it sends entries at `Info` or higher as logs. To change that level, set [`MinimumLogLevel`](/platforms/dotnet/guides/nlog/#minimumloglevel).

```csharp
SentrySdk.Init(options =>
Expand Down
Loading