Skip to content

Commit

Permalink
move nlog back to AsyncLocal reentrancy check (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jul 31, 2022
1 parent 31a4ebe commit e0d6b1d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Sentry.NLog/SentryTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
.FlushAsync(Options.FlushTimeout)
.ContinueWith(t => asyncContinuation(t.Exception));
}

static AsyncLocal<bool> isReentrant = new();
/// <summary>
/// <para>
/// If the event level &gt;= the <see cref="MinimumEventLevel"/>, the
Expand All @@ -290,14 +290,13 @@ protected override void Write(LogEventInfo logEvent)
return;
}

const string sentryContext = "__sentry__";
if (GetContextNdlc(logEvent)?.Contains(sentryContext) is true)
if (isReentrant.Value)
{
Options.DiagnosticLogger?.LogError($"Reentrant log event detected. Logging when inside the scope of another log event can cause a StackOverflowException. LogEventInfo.Message:{logEvent.Message}");
return;
}

using var _ = NestedDiagnosticsLogicalContext.Push(sentryContext);
isReentrant.Value = true;

try
{
Expand All @@ -308,6 +307,10 @@ protected override void Write(LogEventInfo logEvent)
Options.DiagnosticLogger?.LogError("Failed to write log event", exception);
throw;
}
finally
{
isReentrant.Value = false;
}
}

private void InnerWrite(LogEventInfo logEvent)
Expand Down

0 comments on commit e0d6b1d

Please sign in to comment.