Skip to content

NLog target stack overflow when Debug is on and NLog handles ILogger #5822

Closed

Description

@jamescrosswell

Problem

On version7 (since 7.0.0-rc.2), an ASP.NET Core app that sends ILogger through NLog and has Debug turned on crashes with a StackOverflowException on its first captured event.

To reproduce, use Sentry.Samples.AspNetCore.NLog with Sentry__Debug=true and request /. It also happens with plain builder.Host.UseNLog() (no ReplaceLoggerFactory).

Cause

With Debug on, UseSentry swaps the SDK's diagnostic logger for a MelDiagnosticLogger, so the SDK's own diagnostics go out through ILogger. When NLog handles ILogger, they reach SentryTarget.Write:

  1. The SDK logs a diagnostic (for example "Capturing event.") while the target is writing an entry.
  2. That goes through ILogger to NLog, and back into SentryTarget.Write.
  3. The reentrancy guard catches it, then logs "Reentrant log event detected…" through HubAccessor()?.GetSentryOptions()?.DiagnosticLogger, which is the same MelDiagnosticLogger.
  4. That message goes back into SentryTarget.Write, hits the guard again, and so on until the stack overflows.

On main the guard logs through the target's own Options.DiagnosticLogger, which doesn't loop back. #5585 switched it to the hub's diagnostic logger.

The Serilog sink avoids this by skipping Sentry's own logger categories (SentrySdkNamespaces.IsSentrySdk); the NLog target doesn't.

Possible fixes

  • Have the reentrancy guard return without logging through the hub's diagnostic logger.
  • Have the target skip events from Sentry's own categories, as the Serilog sink does.

Found while working on #5763.

Activity

  1. linear-code commented on Oct 28, 2026

    @linear-code
  2. added
    Bug
    and removed on Oct 28, 2026
  3. added a commit that references this issue on Oct 28, 2026
    f5f063e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions