Skip to content

fix: Prevent malformed log_event_data JSON for non-serializable context values#3642

Merged
tippmar-nr merged 2 commits into
mainfrom
fix/log-context-data-json-3641
Jun 15, 2026
Merged

fix: Prevent malformed log_event_data JSON for non-serializable context values#3642
tippmar-nr merged 2 commits into
mainfrom
fix/log-context-data-json-3641

Conversation

@tippmar-nr

@tippmar-nr tippmar-nr commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #3641.

When a log context-data value cannot be serialized by Newtonsoft.Json - for example a self-referencing object graph such as an ASP.NET Core Endpoint placed into a logging scope - LogEventWireModelCollectionJsonConverter fell back to writing the value's raw ToString() through WriteRawValue. WriteRawValue injects text verbatim, so the value landed in the stream unquoted and unescaped:

..."context.SomeKey":Foo.Bar.SomeType ...} ...

That makes the whole log_event_data payload invalid JSON, so the collector rejects the entire batch and every log event harvested in that cycle is dropped - not just the offending line.

Change

  • Use WriteRawValue only on the SerializeObject success path (which already returns valid JSON).
  • In the fallback branches, write the stringified value through WriteValue, which quotes and escapes it. A value that cannot be serialized can no longer corrupt the surrounding payload.

Test coverage

  • Unit tests (LogEventWireModelCollectionJsonConverterTests): a non-serializable (self-referencing) context value, and one whose ToString() also throws, both assert the payload parses as valid JSON and the value is emitted as a quoted string.
  • Integration tests (ContextDataNonSerializableTests, MEL on Core latest/oldest and FW latest): log a message with a non-serializable object in the logging scope and assert every log_event_data payload is valid JSON with the value captured as a quoted string. Verified failing without the fix and passing with it.

Author Checklist

  • Unit tests added
  • Integration tests added
  • Performance implications considered (none expected; same code path, one extra string write)

…xt values

When a log context-data value could not be serialized by Newtonsoft.Json (e.g. a
self-referencing object such as an ASP.NET Core Endpoint placed into a logging scope),
the converter fell back to writing a raw ToString() via WriteRawValue. That emitted an
unquoted, unescaped token, making the entire log_event_data payload invalid JSON so the
collector rejected the whole batch and dropped every log event in it.

Write the fallback value through WriteValue (quoted/escaped) instead, keeping WriteRawValue
only for the SerializeObject success path. Adds unit and MEL integration coverage asserting
the payload stays valid JSON.

Fixes #3641
@tippmar-nr tippmar-nr marked this pull request as ready for review June 15, 2026 18:56
@tippmar-nr tippmar-nr requested a review from a team as a code owner June 15, 2026 18:56
nrcventura
nrcventura previously approved these changes Jun 15, 2026
chynesNR
chynesNR previously approved these changes Jun 15, 2026
@tippmar-nr tippmar-nr dismissed stale reviews from chynesNR and nrcventura via 17d30cf June 15, 2026 20:29
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.99%. Comparing base (3a8be50) to head (17d30cf).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...erters/LogEventWireModelCollectionJsonConverter.cs 60.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3642      +/-   ##
==========================================
+ Coverage   81.84%   81.99%   +0.15%     
==========================================
  Files         509      510       +1     
  Lines       34429    34689     +260     
  Branches     4073     4130      +57     
==========================================
+ Hits        28178    28443     +265     
+ Misses       5282     5273       -9     
- Partials      969      973       +4     
Flag Coverage Δ
Agent 82.93% <60.00%> (+0.16%) ⬆️
Profiler 72.23% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...erters/LogEventWireModelCollectionJsonConverter.cs 97.93% <60.00%> (+8.35%) ⬆️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tippmar-nr tippmar-nr enabled auto-merge (squash) June 15, 2026 21:01
@tippmar-nr tippmar-nr merged commit a7108cb into main Jun 15, 2026
224 of 226 checks passed
@tippmar-nr tippmar-nr deleted the fix/log-context-data-json-3641 branch June 15, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed JSON in log_event_data when a log context-data value is a non-serializable object

4 participants