Skip to content

Commit 6eb5b9b

Browse files
committed
fix(logs): do not capture log when template/parameters are invalid
1 parent dd39fae commit 6eb5b9b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Sentry/SentryStructuredLogger.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,17 @@ private void CaptureLog(SentryLogLevel level, string template, object[]? paramet
127127

128128
_ = TryGetParentSpanId(_hub, _scopeManager, out var parentSpanId);
129129

130-
var message = string.Format(CultureInfo.InvariantCulture, template, parameters ?? []);
130+
string message;
131+
try
132+
{
133+
message = string.Format(CultureInfo.InvariantCulture, template, parameters ?? []);
134+
}
135+
catch (FormatException e)
136+
{
137+
_options.DiagnosticLogger?.LogError(e, "Template string does not match the provided argument.");
138+
return;
139+
}
140+
131141
SentryLog log = new(timestamp, traceId, level, message)
132142
{
133143
Template = template,

test/Sentry.Tests/SentryStructuredLoggerTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ internal sealed class Fixture
1111
{
1212
public Fixture()
1313
{
14+
DiagnosticLogger = new InMemoryDiagnosticLogger();
1415
Hub = Substitute.For<IHub>();
1516
ScopeManager = Substitute.For<IInternalScopeManager>();
16-
Options = new SentryOptions();
17+
Options = new SentryOptions
18+
{
19+
Debug = true,
20+
DiagnosticLogger = DiagnosticLogger,
21+
};
1722
Clock = new MockClock(new DateTimeOffset(2025, 04, 22, 14, 51, 00, TimeSpan.Zero));
1823
Span = Substitute.For<ISpan>();
1924
TraceId = SentryId.Create();
@@ -24,6 +29,7 @@ public Fixture()
2429
Span.ParentSpanId.Returns(ParentSpanId);
2530
}
2631

32+
public InMemoryDiagnosticLogger DiagnosticLogger { get; }
2733
public IHub Hub { get; }
2834
public IInternalScopeManager ScopeManager { get; }
2935
public SentryOptions Options { get; }
@@ -147,6 +153,22 @@ public void Log_WhenBeforeSendLogReturnsNull_DoesNotCaptureEnvelope()
147153
invocations.Should().Be(1);
148154
}
149155

156+
[Fact]
157+
public void Log_InvalidFormat_DoesNotCaptureEnvelope()
158+
{
159+
_fixture.Options.EnableLogs = true;
160+
var logger = _fixture.GetSut();
161+
162+
logger.LogTrace("Template string with arguments: {0}, {1}, {2}, {3}, {4}", ["string", true, 1, 2.2]);
163+
164+
_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
165+
var entry = _fixture.DiagnosticLogger.Entries.Should().ContainSingle().Which;
166+
entry.Level.Should().Be(SentryLevel.Error);
167+
entry.Message.Should().Be("Template string does not match the provided argument.");
168+
entry.Exception.Should().BeOfType<FormatException>();
169+
entry.Args.Should().BeEmpty();
170+
}
171+
150172
private static void ConfigureLog(SentryLog log)
151173
{
152174
log.SetAttribute("attribute-key", "attribute-value");

0 commit comments

Comments
 (0)