---
title: "Logs"
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry using the .NET SDK."
url: https://docs.sentry.io/platforms/dotnet/guides/nlog/logs/
---

# Set Up Logs for NLog

## Set up with Sentry CLI

Set up the Sentry .NET SDK with the Sentry CLI. Install the CLI with one of these commands, then follow the Sentry CLI docs to finish setup.

**cURL**

```bash
curl https://cli.sentry.dev/install -fsS | bash
```

**PowerShell**

```powershell
irm https://cli.sentry.dev/install.ps1 | iex
```

**Homebrew**

```bash
brew install getsentry/tools/sentry
```

**npx**

```bash
npx sentry@latest
```

[Learn more about the Sentry CLI](https://cli.sentry.dev/)

With Sentry Structured Logs, you can send text-based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.

## [Requirements](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#requirements)

Logs for NLog are supported in Sentry NLog SDK version `6.8.0` and above.

## [Setup](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#setup)

Logs sent through the `SentrySdk.Logger` APIs are always sent, so they don't need any extra configuration.

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`](https://docs.sentry.io/platforms/dotnet/guides/nlog.md#minimumloglevel).

```csharp
SentrySdk.Init(options =>
{
    options.Dsn = "https://<key>@o<orgId>.ingest.sentry.io/<projectId>";
});

var config = new LoggingConfiguration();
config.AddSentry();
LogManager.Configuration = config;
```

If the SDK is initialized elsewhere, for example by `UseSentry`, skip `SentrySdk.Init`.

##### Changed in Version 7.0.0

In earlier versions, this integration only sent logs when `EnableLogs` was set to `true` on the options used to initialize the SDK.

In an app that calls `UseSentry`, add NLog with `AddNLog` from the `NLog.Extensions.Logging` package, and set `ReplaceLoggerFactory`:

```csharp
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;

var config = new LoggingConfiguration();
config.AddSentry();
LogManager.Configuration = config;

builder.Logging.AddNLog(new NLogProviderOptions { ReplaceLoggerFactory = true });
builder.UseSentry(options =>
{
    options.Dsn = "https://<key>@o<orgId>.ingest.sentry.io/<projectId>";
});
```

`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`](https://docs.sentry.io/platforms/dotnet/guides/nlog.md#minimumloglevel). In ASP.NET Core, `UseNLog` from the `NLog.Web.AspNetCore` package takes the same option.

It does *not* capture the `Console.WriteLine()` standard output stream.

## [Usage](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#usage)

Once the SDK is initialized and the Sentry target is added, you can send logs using the *NLog* APIs.

The `Logger` type exposes various methods that you can use to log messages at six different log levels automatically mapped to Sentry's severity:

| NLog.LogLevel | Sentry.SentryLogLevel | Sentry Logs UI Severity |
| ------------- | --------------------- | ----------------------- |
| Trace         | Trace                 | trace                   |
| Debug         | Debug                 | debug                   |
| Info          | Info                  | info                    |
| Warn          | Warning               | warn                    |
| Error         | Error                 | error                   |
| Fatal         | Fatal                 | fatal                   |

These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.

```csharp
var logger = LogManager.GetCurrentClassLogger();

logger.Info("A simple log message");
logger.Error("A {Parameter} log message", "formatted");
logger.WithProperty("Property", "Value").Warn("Message with Property");
```

The *properties* of *log events* are attached as attributes to the logs, alongside a set of default attributes automatically provided by the SDK. The logger name is attached as the `category.name` attribute.

## [Options](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#options)

#### [MinimumLogLevel](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#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.

#### [SetBeforeSendLog](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#setbeforesendlog)

To filter logs or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func<SentryLog, SentryLog?>)` option.

```csharp
options =>
{
    options.Dsn = "https://<key>@o<orgId>.ingest.sentry.io/<projectId>";
    // a callback that is invoked before sending a log to Sentry
    options.SetBeforeSendLog(static log =>
    {
        // filter out all info logs
        if (log.Level is SentryLogLevel.Info)
        {
            return null;
        }

        // filter out logs based on some attribute they have
        if (log.TryGetAttribute("suppress", out var attribute) && attribute is true)
        {
            return null;
        }

        // set a custom attribute for all other logs sent to Sentry
        log.SetAttribute("my.attribute", "value");

        return log;
    });
});
```

The `beforeSendLog` delegate receives a log object, and should return the log object if you want it to be sent to Sentry, or `null` if you want to discard it.

The log object of type `SentryLog` has the following members:

| Member                                          | Type                                           | Description                                                                                                                                                                         |
| ----------------------------------------------- | ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Timestamp`                                     | `DateTimeOffset`                               | The timestamp of the log.                                                                                                                                                           |
| `TraceId`                                       | `SentryId`                                     | The trace id of the log.                                                                                                                                                            |
| `Level`                                         | `SentryLogLevel`                               | The severity level of the log. Either `Trace`, `Debug`, `Info`, `Warning`, `Error`, or `Fatal`.                                                                                     |
| `Message`                                       | `string`                                       | The formatted log message.                                                                                                                                                          |
| `Template`                                      | `string?`                                      | The parameterized template string.                                                                                                                                                  |
| `Parameters`                                    | `ImmutableArray<KeyValuePair<string, object>>` | The parameters to the template string.                                                                                                                                              |
| `SpanId`                                        | `SpanId?`                                      | The span id of the span that was active when the log was collected.                                                                                                                 |
| `TryGetAttribute(string key, out object value)` | Method                                         | Gets the attribute value associated with the specified key. Returns `true` if the log contains an attribute with the specified key and it's value is not `null`, otherwise `false`. |
| `SetAttribute(string key, object value)`        | Method                                         | Sets a key-value pair of data attached to the log. Supported types are `string`, `bool`, integers up to a size of 64-bit signed, and floating-point numbers up to a size of 64-bit. |

## [Best Practices](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#best-practices)

### [Wide Events Over Scattered Logs](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#wide-events-over-scattered-logs)

Instead of many thin logs that are hard to correlate, emit one comprehensive log per operation with all relevant context.

This makes debugging dramatically faster — one query returns everything about a specific order, user, or request.

```csharp
// ❌ Scattered thin logs
SentrySdk.Logger.LogInfo("Starting checkout");
SentrySdk.Logger.LogInfo("Validating cart");
SentrySdk.Logger.LogInfo("Processing payment");
SentrySdk.Logger.LogInfo("Checkout complete");

// ✅ One wide event with full context
SentrySdk.Logger.LogInfo(log =>
{
    log.SetAttribute("order_id", order.Id);
    log.SetAttribute("user_id", user.Id);
    log.SetAttribute("user_tier", user.Subscription);
    log.SetAttribute("cart_value", cart.Total);
    log.SetAttribute("item_count", cart.Items.Count);
    log.SetAttribute("payment_method", "stripe");
    log.SetAttribute("duration_ms", stopwatch.ElapsedMilliseconds);
}, "Checkout completed");
```

### [Include Business Context](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#include-business-context)

Add attributes that help you prioritize and debug:

* **User context** — tier, account age, lifetime value
* **Transaction data** — order value, item count
* **Feature state** — active feature flags
* **Request metadata** — endpoint, method, duration

This lets you filter logs by high-value customers or specific features.

```csharp
SentrySdk.Logger.LogInfo(log =>
{
    // User context
    log.SetAttribute("user_id", user.Id);
    log.SetAttribute("user_tier", user.Plan); // "free" | "pro" | "enterprise"
    log.SetAttribute("account_age_days", user.AgeDays);

    // Request data
    log.SetAttribute("endpoint", "/api/orders");
    log.SetAttribute("method", "POST");
    log.SetAttribute("duration_ms", 234);

    // Business context
    log.SetAttribute("order_value", 149.99);
}, "API request completed");
```

### [Consistent Attribute Naming](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#consistent-attribute-naming)

Pick a naming convention and stick with it across your codebase. Inconsistent names make queries impossible.

**Recommended:** Use `snake_case` for custom attributes to match common conventions.

```csharp
// ❌ Inconsistent naming
log.SetAttribute("user", "123");
log.SetAttribute("userId", "123");
log.SetAttribute("user_id", "123");
log.SetAttribute("UserID", "123");

// ✅ Consistent snake_case
SentrySdk.Logger.LogInfo(log =>
{
    log.SetAttribute("user_id", "123");
    log.SetAttribute("order_id", "456");
    log.SetAttribute("cart_value", 99.99);
    log.SetAttribute("item_count", 3);
}, "Order processed");
```

## [Default Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#default-attributes)

The .NET SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.version`.

### [Message Template Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#message-template-attributes)

If the log was parameterized, Sentry adds the message template and parameters as log attributes.

* `message.template`: The parameterized template string. This is sent from the SDK as `sentry.message.template`.
* `message.parameter.X`: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (`sentry.message.parameter.0`, `sentry.message.parameter.1`, etc) or the parameter's name (`sentry.message.parameter.item_id`, `sentry.message.parameter.user_id`, etc). This is sent from the SDK as `sentry.message.parameter.X`.

### [Server Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#server-attributes)

* `server.address`: The address of the server that sent the log. Equivalent to `server_name` that gets attached to Sentry errors.

### [User Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#user-attributes)

If user information is available in the current scope, the following attributes are added to the log:

* `user.id`: The user ID.
* `user.name`: The username.
* `user.email`: The email address.

### [Integration Attributes](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#integration-attributes)

If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.

* `origin`: The origin of the log. This is sent from the SDK as `sentry.origin`.

## [Other Logging Integrations](https://docs.sentry.io/platforms/dotnet/guides/nlog/logs.md#other-logging-integrations)

Available integrations:

* [ASP.NET Core](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/logs.md)
* [.NET Multi-platform App UI (.NET MAUI)](https://docs.sentry.io/platforms/dotnet/guides/maui/logs.md)
* [Generic Host](https://docs.sentry.io/platforms/dotnet/guides/extensions-hosting/logs.md)
* [Microsoft.Extensions.Logging](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/logs.md)
* [Serilog](https://docs.sentry.io/platforms/dotnet/guides/serilog/logs.md)
* [log4net](https://docs.sentry.io/platforms/dotnet/guides/log4net/logs.md)

If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-dotnet/issues/new/choose).
