Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
- The SDK now provides a `IsSessionActive` to allow checking the session state ([#4662](https://github.com/getsentry/sentry-dotnet/pull/4662))
- 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))

### Fixes

- The `Serilog` integration captures _Structured Logs_ (when enabled) independently of captured Events and added Breadcrumbs ([#4691](https://github.com/getsentry/sentry-dotnet/pull/4691))
Copy link
Member Author

Choose a reason for hiding this comment

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

question: 6.0.0 vs 5.16.3

Do we want to publish this fix in a 5.16.3 patch release,
or is it fine to include in 6.0.0 as it's just a week away.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we plan to GA structured logging in version 5 of the SDK right? So doesn't make sense to backport the fix for this.


## 6.0.0-preview.2

### BREAKING CHANGES
Expand Down
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 addedBreadcrumbForException = 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;
addedBreadcrumbForException = true;
}
}

if (logEvent.Level >= _options.MinimumBreadcrumbLevel)
if (!addedBreadcrumbForException && 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