Skip to content

Add ActivityContext capture to LogEvent for trace correlation#7995

Merged
Aaronontheweb merged 10 commits intoakkadotnet:devfrom
Aaronontheweb:feature/logevent-activity-context
Jan 25, 2026
Merged

Add ActivityContext capture to LogEvent for trace correlation#7995
Aaronontheweb merged 10 commits intoakkadotnet:devfrom
Aaronontheweb:feature/logevent-activity-context

Conversation

@Aaronontheweb
Copy link
Member

Summary

  • Adds ActivityContext? property to LogEvent base class
  • Captures Activity.Current?.Context at log event creation time (before mailbox crossing)
  • Enables downstream logging backends (like OpenTelemetry) to correlate logs with their parent trace/span

Motivation

Related to #6855 - Activity.Current uses AsyncLocal<T> which doesn't flow across actor mailbox boundaries. By capturing the ActivityContext at log event creation time, we preserve the trace context for use by logging backends like LoggerFactoryLogger in Akka.Hosting.

Changes

  • LogEvent.cs - Added ActivityContext? property with capture in constructor
  • Akka.csproj - Added System.Diagnostics.DiagnosticSource package for netstandard2.0
  • Added unit tests validating trace context capture behavior
  • Updated API approval baselines

Test plan

  • Unit tests for ActivityContext capture when Activity.Current exists
  • Unit tests for null ActivityContext when no activity
  • Unit tests verifying capture happens at creation time
  • All existing tests pass
  • API approval tests updated

Captures Activity.Current?.Context at LogEvent creation time, enabling
downstream logging backends (like OpenTelemetry) to correlate logs with
their parent trace/span even after async dispatch through the actor mailbox.

This addresses GitHub issue akkadotnet#6855 where Activity.Current is lost when log
events cross actor mailbox boundaries due to AsyncLocal not flowing.

Changes:
- Add ActivityContext? property to LogEvent base class
- Capture context in constructor before mailbox crossing
- Add System.Diagnostics.DiagnosticSource package for netstandard2.0
- Add unit tests for trace context capture behavior
- Update API approval baselines
@Aaronontheweb Aaronontheweb marked this pull request as draft January 14, 2026 22:25
@Aaronontheweb Aaronontheweb added this to the 1.5.59 milestone Jan 14, 2026
Copy link
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

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

LGTM

@Aaronontheweb Aaronontheweb marked this pull request as ready for review January 23, 2026 15:20
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) January 24, 2026 05:13
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) January 25, 2026 04:33
@Aaronontheweb Aaronontheweb merged commit ed9fffa into akkadotnet:dev Jan 25, 2026
12 checks passed
@Aaronontheweb Aaronontheweb deleted the feature/logevent-activity-context branch January 25, 2026 13:36
Aaronontheweb added a commit to Aaronontheweb/akka.net that referenced this pull request Jan 26, 2026
…tnet#7995)

Captures Activity.Current?.Context at LogEvent creation time, enabling
downstream logging backends (like OpenTelemetry) to correlate logs with
their parent trace/span even after async dispatch through the actor mailbox.

This addresses GitHub issue akkadotnet#6855 where Activity.Current is lost when log
events cross actor mailbox boundaries due to AsyncLocal not flowing.

Changes:
- Add ActivityContext? property to LogEvent base class
- Capture context in constructor before mailbox crossing
- Add System.Diagnostics.DiagnosticSource package for netstandard2.0
- Add unit tests for trace context capture behavior
- Update API approval baselines

Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
Aaronontheweb added a commit that referenced this pull request Jan 26, 2026
Captures Activity.Current?.Context at LogEvent creation time, enabling
downstream logging backends (like OpenTelemetry) to correlate logs with
their parent trace/span even after async dispatch through the actor mailbox.

This addresses GitHub issue #6855 where Activity.Current is lost when log
events cross actor mailbox boundaries due to AsyncLocal not flowing.

Changes:
- Add ActivityContext? property to LogEvent base class
- Capture context in constructor before mailbox crossing
- Add System.Diagnostics.DiagnosticSource package for netstandard2.0
- Add unit tests for trace context capture behavior
- Update API approval baselines

Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
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.

2 participants