Commit b1b0738
authored
Fix Serilog direct log submission failing when writing unknown custom properties (#8010)
## Summary of changes
This fixes a `JsonWriterException` that we were hitting when attempting
to serialize custom properties in Serilog log messages when we had
Direct Log Submission enabled. This would then result in direct log
submission to stop working.
## Reason for change
When writing a custom property in Serilog we only write the JSON when
Debug logging is enabled, when it isn't enabled we actually end up
putting the writer into an invalid state, we then hit the following
exception:
```
2025-12-23 16:04:16.209 -05:00 [ERR] An error occured sending logs to Datadog Datadog.Trace.Vendors.Newtonsoft.Json.JsonWriterException: Token PropertyName in state Property would result in an invalid JSON object. Path ''.
at Datadog.Trace.Vendors.Newtonsoft.Json.JsonWriter.AutoComplete(JsonToken tokenBeingWritten) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\Vendors\Newtonsoft.Json\JsonWriter.cs:line 880
at Datadog.Trace.Vendors.Newtonsoft.Json.JsonWriter.InternalWritePropertyName(String name) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\Vendors\Newtonsoft.Json\JsonWriter.cs:line 1760
at Datadog.Trace.Vendors.Newtonsoft.Json.JsonTextWriter.WritePropertyName(String name, Boolean escape) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\Vendors\Newtonsoft.Json\JsonTextWriter.cs:line 291
at Datadog.Trace.Logging.DirectSubmission.Formatting.LogFormatter.FormatLog[T](StringBuilder builder, T& state, DateTime timestamp, String message, Nullable`1 eventId, String logLevel, Exception exception, FormatDelegate`1 renderPropertiesDelegate) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\Logging\DirectSubmission\Formatting\LogFormatter.cs:line 335
at Datadog.Trace.ClrProfiler.AutoInstrumentation.Logging.Serilog.DirectSubmission.Formatting.SerilogLogFormatter.FormatLogEvent(LogFormatter logFormatter, StringBuilder sb, ILogEvent logEvent) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\ClrProfiler\AutoInstrumentation\Logging\Serilog\DirectSubmission\Formatting\SerilogLogFormatter.cs:line 26
at Datadog.Trace.ClrProfiler.AutoInstrumentation.Logging.Serilog.DirectSubmission.SerilogDirectSubmissionLogEvent.Format(StringBuilder sb, LogFormatter formatter) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\ClrProfiler\AutoInstrumentation\Logging\Serilog\DirectSubmission\SerilogDirectSubmissionLogEvent.cs:line 25
at Datadog.Trace.Logging.DirectSubmission.Sink.DirectSubmissionLogSink.EmitBatch(Queue`1 events) in C:\Users\steven.bouwkamp\source\repos\dd-trace-dotnet2\tracer\src\Datadog.Trace\Logging\DirectSubmission\Sink\DirectSubmissionLogSink.cs:line 103
{ MachineName: ".", Process: "[100360 dotnet]", AppDomain: "[1 LogsInjection.Serilog]", AssemblyLoadContext: "\"\" Datadog.Trace.ClrProfiler.Managed.Loader.ManagedProfilerAssemblyLoadContext #1", TracerVersion: "3.35.0.0" }
```
From what I can tell this causes direct log submission to stop working.
I've seen this in Error Tracking and it may be related to a customer
escalation regarding issues with Serilog direct log submission to
seemingly stop working.
## Implementation details
## Test coverage
I've reproduced this, but it is strange, I guess Serilog doesn't
actually support this? I don't understand completely what was going on,
but if I added a custom property and enriched all logs with it but did
_not_ have direct log submission enabled there would be no logs found in
the tests but they did make it to the file. 😕
## Other details
<!-- Fixes #{issue} -->
I only toggled this custom property on when direct log submission is
enabled due to the above Test Coverage note.
<!-- 1 parent e2908cf commit b1b0738
File tree
2 files changed
+52
-3
lines changed- tracer
- src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Serilog/DirectSubmission/Formatting
- test/test-applications/integrations/LogsInjection.Serilog
2 files changed
+52
-3
lines changedLines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | | - | |
113 | | - | |
| 115 | + | |
114 | 116 | | |
115 | 117 | | |
116 | 118 | | |
| |||
Lines changed: 47 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
18 | 54 | | |
19 | 55 | | |
20 | 56 | | |
| |||
81 | 117 | | |
82 | 118 | | |
83 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
84 | 131 | | |
85 | 132 | | |
86 | 133 | | |
| |||
0 commit comments