Set Up Logs

Structured logs allow you to send, view and query logs sent from your applications within Sentry using the .NET SDK.

Agent-Assisted Setup
Use curl to download, read and follow https://skills.sentry.dev/instrument to set up the Sentry .NET SDK.
Your agent will set up Sentry in your .NET app automatically. Works with Cursor, Claude Code, Codex, and more.View docs ↗
Install the full plugin

Install the Sentry plugin to give your assistant every skill. See the installation docs for more details.

Copied
npx @sentry/agent-plugin install

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.

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

To enable logging, you need to initialize the SDK with the EnableLogs option set to true.

EnableLogs isn't exposed as a log4net appender setting, so initialize the SDK with SentrySdk.Init and leave the DSN out of your log4net appender configuration.

Copied
SentrySdk.Init(options =>
{
    options.Dsn = "___PUBLIC_DSN___";
    // Enable logs to be sent to Sentry
    options.EnableLogs = true;
});

The SentryAppender configured in your log4net configuration then captures logs without initializing the SDK itself. If the SDK is initialized elsewhere (for example, through ASP.NET Core), enable EnableLogs on those options instead.

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

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the log4net APIs, for example through the ILog methods (Debug, Info, Warn, Error, and Fatal).

log4net levels are automatically mapped to Sentry's severity:

log4net.Core.LevelSentry.SentryLogLevelSentry Logs UI Severity
TraceTracetrace
DebugDebugdebug
InfoInfoinfo
WarnWarningwarn
ErrorErrorerror
FatalFatalfatal

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.

Copied
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

Log.Info("A simple log message");
Log.Error("An error log message");

ThreadContext.Properties["Property"] = "Value";
Log.Warn("Message with a custom property");

The log4net properties (from the log event, ThreadContext, and GlobalContext) are attached as property.<name> 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.

The SentryAppender's Environment and SendIdentity settings also apply to structured logs: when configured, Environment sets the log's environment and SendIdentity attaches the log4net identity as the user.

Because log4net renders each message before it reaches the appender, structured logs from log4net carry the fully-formatted message but no message template or parameters.

Set to true in order to enable the logging integration via the log4net ILog APIs.

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

Copied
options =>
{
    options.Dsn = "___PUBLIC_DSN___";
    options.EnableLogs = true;
    // 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:

MemberTypeDescription
TimestampDateTimeOffsetThe timestamp of the log.
TraceIdSentryIdThe trace id of the log.
LevelSentryLogLevelThe severity level of the log. Either Trace, Debug, Info, Warning, Error, or Fatal.
MessagestringThe formatted log message.
Templatestring?The parameterized template string.
ParametersImmutableArray<KeyValuePair<string, object>>The parameters to the template string.
SpanIdSpanId?The span id of the span that was active when the log was collected.
TryGetAttribute(string key, out object value)MethodGets 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)MethodSets 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.

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.

Copied
// ❌ 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");

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.

Copied
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");

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.

Copied
// ❌ 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");

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

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

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.address: The address of the server that sent the log. Equivalent to server_name that gets attached to Sentry errors.

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.

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.

Available integrations:

If there's an integration you would like to see, open a new issue on GitHub.

Was this helpful?
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").