Skip to content

Commit 130650d

Browse files
authored
fix: Serilog integration captures structured log when event with exception has been captured (#4691)
1 parent a739982 commit 130650d

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
- The SDK now provides a `IsSessionActive` to allow checking the session state ([#4662](https://github.com/getsentry/sentry-dotnet/pull/4662))
1515
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633))
1616

17+
### Fixes
18+
19+
- The `Serilog` integration captures _Structured Logs_ (when enabled) independently of captured Events and added Breadcrumbs ([#4691](https://github.com/getsentry/sentry-dotnet/pull/4691))
20+
1721
## 6.0.0-preview.2
1822

1923
### BREAKING CHANGES

src/Sentry.Serilog/SentrySink.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private void InnerEmit(LogEvent logEvent)
105105
var exception = logEvent.Exception;
106106
var template = logEvent.MessageTemplate.Text;
107107
var formatted = FormatLogEvent(logEvent);
108+
var addedBreadcrumbForException = false;
108109

109110
if (logEvent.Level >= _options.MinimumEventLevel)
110111
{
@@ -137,11 +138,11 @@ private void InnerEmit(LogEvent logEvent)
137138
// Capturing exception events adds a breadcrumb automatically... we don't want to add another one
138139
if (exception != null)
139140
{
140-
return;
141+
addedBreadcrumbForException = true;
141142
}
142143
}
143144

144-
if (logEvent.Level >= _options.MinimumBreadcrumbLevel)
145+
if (!addedBreadcrumbForException && logEvent.Level >= _options.MinimumBreadcrumbLevel)
145146
{
146147
Dictionary<string, string>? data = null;
147148
if (exception != null && !string.IsNullOrWhiteSpace(formatted))

test/Sentry.Serilog.Tests/SentrySinkTests.Structured.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,36 @@ public void Emit_StructuredLogging_LogEvent(bool withActiveSpan)
134134
log.TryGetAttribute("property.Structure-Property", out object? structure).Should().BeTrue();
135135
structure.Should().Be("""[42, "42"]""");
136136
}
137+
138+
[Fact]
139+
public void Emit_StructuredLoggingWithException_NoBreadcrumb()
140+
{
141+
InMemorySentryStructuredLogger capturer = new();
142+
_fixture.Hub.Logger.Returns(capturer);
143+
_fixture.Options.Experimental.EnableLogs = true;
144+
145+
var sut = _fixture.GetSut();
146+
var logger = new LoggerConfiguration().WriteTo.Sink(sut).MinimumLevel.Verbose().CreateLogger();
147+
148+
logger.Write(LogEventLevel.Error, new Exception("expected message"), "Message");
149+
150+
_fixture.Scope.Breadcrumbs.Should().BeEmpty();
151+
capturer.Logs.Should().ContainSingle().Which.Message.Should().Be("Message");
152+
}
153+
154+
[Fact]
155+
public void Emit_StructuredLoggingWithoutException_LeavesBreadcrumb()
156+
{
157+
InMemorySentryStructuredLogger capturer = new();
158+
_fixture.Hub.Logger.Returns(capturer);
159+
_fixture.Options.Experimental.EnableLogs = true;
160+
161+
var sut = _fixture.GetSut();
162+
var logger = new LoggerConfiguration().WriteTo.Sink(sut).MinimumLevel.Verbose().CreateLogger();
163+
164+
logger.Write(LogEventLevel.Error, (Exception?)null, "Message");
165+
166+
_fixture.Scope.Breadcrumbs.Should().ContainSingle().Which.Message.Should().Be("Message");
167+
capturer.Logs.Should().ContainSingle().Which.Message.Should().Be("Message");
168+
}
137169
}

0 commit comments

Comments
 (0)