diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df0f5694f..0c54cbcc8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- Templates are no longer sent with Structured Logs that have no parameters ([#4544](https://github.com/getsentry/sentry-dotnet/pull/4544)) - Upload linked PDBs to fix non-IL-stripped symbolication for iOS ([#4527](https://github.com/getsentry/sentry-dotnet/pull/4527)) - In MAUI Android apps, generate and inject UUID to APK and upload ProGuard mapping to Sentry with the UUID ([#4532](https://github.com/getsentry/sentry-dotnet/pull/4532)) - Fixed WASM0001 warning when building Blazor WebAssembly projects ([#4519](https://github.com/getsentry/sentry-dotnet/pull/4519)) diff --git a/src/Sentry/SentryLog.cs b/src/Sentry/SentryLog.cs index b506b9da6c..7aef142d36 100644 --- a/src/Sentry/SentryLog.cs +++ b/src/Sentry/SentryLog.cs @@ -224,7 +224,9 @@ internal void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) writer.WritePropertyName("attributes"); writer.WriteStartObject(); - if (Template is not null) + // the SDK MUST NOT attach a sentry.message.template attribute if there are no parameters + // https://develop.sentry.dev/sdk/telemetry/logs/#default-attributes + if (Template is not null && !Parameters.IsDefaultOrEmpty) { SentryAttributeSerializer.WriteStringAttribute(writer, "sentry.message.template", Template); } diff --git a/test/Sentry.Tests/SentryLogTests.cs b/test/Sentry.Tests/SentryLogTests.cs index 3393137b85..105e196d56 100644 --- a/test/Sentry.Tests/SentryLogTests.cs +++ b/test/Sentry.Tests/SentryLogTests.cs @@ -67,6 +67,30 @@ public void Protocol_Default_VerifyAttributes() notFound.Should().BeNull(); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public void WriteTo_NoParameters_NoTemplate(bool hasParameters) + { + // Arrange + ImmutableArray> parameters = hasParameters + ? [new KeyValuePair("param", "params")] + : []; + var log = new SentryLog(Timestamp, TraceId, SentryLogLevel.Debug, "message") + { + Template = "template", + Parameters = parameters, + ParentSpanId = ParentSpanId, + }; + + // Act + var document = log.ToJsonDocument(static (obj, writer, logger) => obj.WriteTo(writer, logger), _output); + var attributes = document.RootElement.GetProperty("attributes"); + + // Assert + attributes.TryGetProperty("sentry.message.template", out _).Should().Be(hasParameters); + } + [Fact] public void WriteTo_Envelope_MinimalSerializedSentryLog() {