Skip to content

Capture trace context, thread ID and message template in buffered logs - #7818

Open
evgenyfedorov2 wants to merge 1 commit into
mainfrom
evgenyfedorov2-log-buffering-trace-context
Open

evgenyfedorov2 wants to merge 1 commit into
mainfrom
evgenyfedorov2-log-buffering-trace-context

Conversation

@evgenyfedorov2

@evgenyfedorov2 evgenyfedorov2 commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Adds ActivityTraceId, ActivitySpanId, ManagedThreadId and MessageTemplate to buffered logs. Previously they were dropped.

Consequently, we don't need two identical internal types SerializedLogRecord and DeserializedLogRecord, so one of them is removed.

Fixes: #6502

Also in this PR

  • Records logged without an exception reported an empty exception message instead of null. Loggers that don't implement IBufferedLogger received new Exception("") for every flushed record, FakeLogRecord.Exception was set, and the console formatters printed an empty exception.

Worth a look

  • Only loggers implementing IBufferedLogger see those new values. Loggers that don't implement it still see the flushing thread's Activity.Current.
Microsoft Reviewers: Open in CodeFlow

@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 🔻
Microsoft.Extensions.AI.OpenAI Line 75 68.32 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 57.59 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.03 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 71.61 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 68.11 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Http.Diagnostics 94 95
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 85
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1628010&view=codecoverage-tab

@evgenyfedorov2
evgenyfedorov2 marked this pull request as ready for review October 9, 2026 14:08
@evgenyfedorov2
evgenyfedorov2 requested review from a team as code owners October 9, 2026 14:08
Copilot AI balanced review requested due to automatic review settings October 9, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

4 open findings
What changed in this PR

This PR updates log buffering to preserve additional original-record context (message template, trace/span IDs, managed thread ID) when flushing, and adds tests to validate the behavior.

Changes:

  • Add new unit tests for global and per-request log buffer flushing behavior.
  • Refactor buffered record representation to use pooled SerializedLogRecord instances and capture thread/activity context at record creation time.
  • Update buffer flush paths to emit SerializedLogRecord directly (removing DeserializedLogRecord).
File Description
test/​Libraries/​Microsoft.Extensions.Telemetry.Tests/​Buffering/​GlobalBufferTests.cs Adds coverage for global-buffer flush preserving activity/thread/message-template/exception behavior.
test/​Libraries/​Microsoft.AspNetCore.Diagnostics.Middleware.Tests/​Buffering/​IncomingRequestLogBufferTests.cs Adds coverage for per-request buffer flush preserving activity/thread/message-template behavior.
src/​Shared/​LogBuffering/​SerializedLogRecordFactory.cs Captures message template + W3C activity IDs + thread ID at record creation and introduces record pooling.
src/​Shared/​LogBuffering/​SerializedLogRecord.cs Changes SerializedLogRecord to a pooled reference type implementing BufferedLogRecord properties.
src/​Shared/​LogBuffering/​DeserializedLogRecord.cs Removes the now-unused deserialized wrapper record type.
src/​Libraries/​Microsoft.Extensions.Telemetry/​README.md Updates documentation about what buffered record data is preserved/available.
src/​Libraries/​Microsoft.Extensions.Telemetry/​Buffering/​GlobalBuffer.cs Flush now emits SerializedLogRecord instances directly instead of creating DeserializedLogRecord.
src/​Libraries/​Microsoft.AspNetCore.Diagnostics.Middleware/​Buffering/​IncomingRequestLogBuffer.cs Flush now emits SerializedLogRecord instances directly instead of creating DeserializedLogRecord.

🧠 Review effort: Lite


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Libraries/Microsoft.Extensions.Telemetry/README.md
Comment thread src/Shared/LogBuffering/SerializedLogRecordFactory.cs
Adds ActivityTraceId, ActivitySpanId, ManagedThreadId and MessageTemplate
to buffered logs. Previously they were dropped.

Consequently, we don't need the two identical internal types
SerializedLogRecord and DeserializedLogRecord, so one of them is removed.

Also, records logged without an exception now report no exception
instead of an empty one.

Fixes #6502

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@evgenyfedorov2
evgenyfedorov2 force-pushed the evgenyfedorov2-log-buffering-trace-context branch from d797bf4 to 1783aaa Compare October 9, 2026 14:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Consider capturing ActivitySpanId, ActivityTraceId, and MessageTemplate when using log buffering

3 participants