Skip to content

Commit 69c05b8

Browse files
committed
fix(logs): do not capture log on user callback exceptions
1 parent 6eb5b9b commit 69c05b8

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/Sentry/SentryStructuredLogger.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void CaptureLog(SentryLogLevel level, string template, object[]? paramet
134134
}
135135
catch (FormatException e)
136136
{
137-
_options.DiagnosticLogger?.LogError(e, "Template string does not match the provided argument.");
137+
_options.DiagnosticLogger?.LogError(e, "Template string does not match the provided argument. The Log will be dropped.");
138138
return;
139139
}
140140

@@ -151,9 +151,7 @@ private void CaptureLog(SentryLogLevel level, string template, object[]? paramet
151151
}
152152
catch (Exception e)
153153
{
154-
//TODO: change to Diagnostic Logger (if enabled)
155-
// see https://github.com/getsentry/sentry-dotnet/issues/4132
156-
Console.WriteLine(e);
154+
_options.DiagnosticLogger?.LogError(e, "The configureLog callback threw an exception. The Log will be dropped.");
157155
return;
158156
}
159157

@@ -168,9 +166,7 @@ private void CaptureLog(SentryLogLevel level, string template, object[]? paramet
168166
}
169167
catch (Exception e)
170168
{
171-
//TODO: change to Diagnostic Logger (if enabled)
172-
// see https://github.com/getsentry/sentry-dotnet/issues/4132
173-
Console.WriteLine(e);
169+
_options.DiagnosticLogger?.LogError(e, "The BeforeSendLog callback threw an exception. The Log will be dropped.");
174170
return;
175171
}
176172
}

test/Sentry.Tests/SentryStructuredLoggerTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,53 @@ public void Log_InvalidFormat_DoesNotCaptureEnvelope()
164164
_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
165165
var entry = _fixture.DiagnosticLogger.Entries.Should().ContainSingle().Which;
166166
entry.Level.Should().Be(SentryLevel.Error);
167-
entry.Message.Should().Be("Template string does not match the provided argument.");
167+
entry.Message.Should().Be("Template string does not match the provided argument. The Log will be dropped.");
168168
entry.Exception.Should().BeOfType<FormatException>();
169169
entry.Args.Should().BeEmpty();
170170
}
171171

172+
[Fact]
173+
public void Log_InvalidConfigureLog_DoesNotCaptureEnvelope()
174+
{
175+
_fixture.Options.EnableLogs = true;
176+
var logger = _fixture.GetSut();
177+
178+
logger.LogTrace("Template string with arguments: {0}, {1}, {2}, {3}", ["string", true, 1, 2.2], Throw);
179+
180+
_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
181+
var entry = _fixture.DiagnosticLogger.Entries.Should().ContainSingle().Which;
182+
entry.Level.Should().Be(SentryLevel.Error);
183+
entry.Message.Should().Be("The configureLog callback threw an exception. The Log will be dropped.");
184+
entry.Exception.Should().BeOfType<InvalidOperationException>();
185+
entry.Args.Should().BeEmpty();
186+
}
187+
188+
[Fact]
189+
public void Log_InvalidBeforeSendLog_DoesNotCaptureEnvelope()
190+
{
191+
_fixture.Options.EnableLogs = true;
192+
_fixture.Options.SetBeforeSendLog(static (SentryLog log) => throw new InvalidOperationException());
193+
var logger = _fixture.GetSut();
194+
195+
logger.LogTrace("Template string with arguments: {0}, {1}, {2}, {3}", ["string", true, 1, 2.2]);
196+
197+
_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
198+
var entry = _fixture.DiagnosticLogger.Entries.Should().ContainSingle().Which;
199+
entry.Level.Should().Be(SentryLevel.Error);
200+
entry.Message.Should().Be("The BeforeSendLog callback threw an exception. The Log will be dropped.");
201+
entry.Exception.Should().BeOfType<InvalidOperationException>();
202+
entry.Args.Should().BeEmpty();
203+
}
204+
172205
private static void ConfigureLog(SentryLog log)
173206
{
174207
log.SetAttribute("attribute-key", "attribute-value");
175208
}
209+
210+
private static void Throw(SentryLog log)
211+
{
212+
throw new InvalidOperationException();
213+
}
176214
}
177215

178216
file static class AssertionExtensions

0 commit comments

Comments
 (0)