Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(logs): Serilog integration captures log when Event with Exception…
… has been captured
  • Loading branch information
Flash0ver committed Nov 4, 2025
commit 769a3adb2757489715f7cc588c3096b600e75f97
5 changes: 3 additions & 2 deletions src/Sentry.Serilog/SentrySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private void InnerEmit(LogEvent logEvent)
var exception = logEvent.Exception;
var template = logEvent.MessageTemplate.Text;
var formatted = FormatLogEvent(logEvent);
Comment on lines 106 to 107

This comment was marked as outdated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

-addedBreadcrumb
+addedBreadcrumbForException

to go in line with the Hub.AddBreadcrumbForException method, which is implicitly adding the Breadcrumb when an Exception is present.

var addedBreadcrumb = false;

if (logEvent.Level >= _options.MinimumEventLevel)
{
Expand Down Expand Up @@ -137,11 +138,11 @@ private void InnerEmit(LogEvent logEvent)
// Capturing exception events adds a breadcrumb automatically... we don't want to add another one
if (exception != null)
{
return;
addedBreadcrumb = true;
}
}

if (logEvent.Level >= _options.MinimumBreadcrumbLevel)
if (!addedBreadcrumb && logEvent.Level >= _options.MinimumBreadcrumbLevel)
{
Dictionary<string, string>? data = null;
if (exception != null && !string.IsNullOrWhiteSpace(formatted))
Expand Down
32 changes: 32 additions & 0 deletions test/Sentry.Serilog.Tests/SentrySinkTests.Structured.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,36 @@ public void Emit_StructuredLogging_LogEvent(bool withActiveSpan)
log.TryGetAttribute("property.Structure-Property", out object? structure).Should().BeTrue();
structure.Should().Be("""[42, "42"]""");
}

[Fact]
public void Emit_StructuredLoggingWithException_NoBreadcrumb()
{
InMemorySentryStructuredLogger capturer = new();
_fixture.Hub.Logger.Returns(capturer);
_fixture.Options.Experimental.EnableLogs = true;

var sut = _fixture.GetSut();
var logger = new LoggerConfiguration().WriteTo.Sink(sut).MinimumLevel.Verbose().CreateLogger();

logger.Write(LogEventLevel.Error, new Exception("expected message"), "Message");

_fixture.Scope.Breadcrumbs.Should().BeEmpty();
capturer.Logs.Should().ContainSingle().Which.Message.Should().Be("Message");
}

[Fact]
public void Emit_StructuredLoggingWithoutException_LeavesBreadcrumb()
{
InMemorySentryStructuredLogger capturer = new();
_fixture.Hub.Logger.Returns(capturer);
_fixture.Options.Experimental.EnableLogs = true;

var sut = _fixture.GetSut();
var logger = new LoggerConfiguration().WriteTo.Sink(sut).MinimumLevel.Verbose().CreateLogger();

logger.Write(LogEventLevel.Error, (Exception?)null, "Message");

_fixture.Scope.Breadcrumbs.Should().ContainSingle().Which.Message.Should().Be("Message");
capturer.Logs.Should().ContainSingle().Which.Message.Should().Be("Message");
}
}
Loading