Skip to content
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Templates are no longer sent with Structured that have no parameters ([#4544](https://github.com/getsentry/sentry-dotnet/pull/4544))

## 5.15.1

### Fixes
Expand Down
4 changes: 3 additions & 1 deletion src/Sentry/Internal/DefaultSentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ private protected override void CaptureLog(SentryLogLevel level, string template

SentryLog log = new(timestamp, traceHeader.TraceId, level, message)
{
Template = template,
// the SDK MUST NOT attach a sentry.message.template attribute if there are no parameters
// https://develop.sentry.dev/sdk/telemetry/logs/#default-attributes
Template = parameters is { Length: > 0 } ? template : null,
Parameters = @params,
ParentSpanId = traceHeader.SpanId,
};
Expand Down
34 changes: 34 additions & 0 deletions test/Sentry.Tests/SentryStructuredLoggerTests.Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ public void Log_Enabled_CapturesEnvelope(SentryLogLevel level)
_fixture.AssertEnvelopeWithoutAttributes(envelope, level);
}

[Theory]
[InlineData(SentryLogLevel.Trace)]
[InlineData(SentryLogLevel.Debug)]
[InlineData(SentryLogLevel.Info)]
[InlineData(SentryLogLevel.Warning)]
[InlineData(SentryLogLevel.Error)]
[InlineData(SentryLogLevel.Fatal)]
public void Log_NoParameters_TemplateNotSet(SentryLogLevel level)
{
// Arrange
_fixture.Options.Experimental.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
_fixture.Hub.CaptureEnvelope(Arg.Do<Envelope>(arg => envelope = arg));

// Act
logger.Log(level, "Template string without arguments");
logger.Flush();

// Assert
_fixture.Hub.Received(1).CaptureEnvelope(Arg.Any<Envelope>());
var envelopeItem = envelope.Items.Should().ContainSingle().Which;
var log = envelopeItem.Payload.Should().BeOfType<JsonSerializable>().Which.Source.Should().BeOfType<StructuredLog>().Which;
log.Should().NotBeNull();
var items = log.Items;
items.Length.Should().Be(1);
var item = items[0];

// If there are no sentry.message.parameter.X attributes included in the log, then
// the SDK MUST NOT attach a sentry.message.template attribute.
item.Template.Should().BeNull();
}

[Theory]
[InlineData(SentryLogLevel.Trace)]
[InlineData(SentryLogLevel.Debug)]
Expand Down
Loading