Skip to content

feat: Include structured log message arguments as context data for MEL#3479

Open
nr-ahemsath wants to merge 12 commits intomainfrom
feature/mel-structured-logging-as-context-data
Open

feat: Include structured log message arguments as context data for MEL#3479
nr-ahemsath wants to merge 12 commits intomainfrom
feature/mel-structured-logging-as-context-data

Conversation

@nr-ahemsath
Copy link
Copy Markdown
Member

@nr-ahemsath nr-ahemsath commented Mar 12, 2026

Description

Fixes #2519

When using Microsoft.Extensions.Logging (MEL) with structured logging (e.g., logger.LogInformation("User {UserId} did {Action}", userId, action)), the named parameters are now extracted from the TState state parameter and merged into context data. This makes them available as individual log attributes in New Relic (e.g., context.UserId, context.Action), matching the behavior already available for Serilog and NLog.

The state extraction is wrapped in its own try/catch to ensure it cannot disrupt existing scope-based context data harvesting. The {OriginalFormat} key (the message template string) is excluded.

Testing

Adds StructuredLogArgContextDataTests that verifies structured log message arguments (e.g., {Name}, {Id}) are extracted as context data attributes for supported use cases:

  • MEL by itself
  • MEL with Serilog
  • MEL with NLog
  • Serilog by itself
  • NLog by itself

Log4Net is purposefully not tested because it doesn't support structured logging.

Additionally, some refactoring of the logging testing system is done for clarity, particularly around which testing frameworks are supported in which TFMs we build the ConsoleMF system for, and making the main code path for logging messages in LoggingTester easier to understand (there was a lot of entanglement with the context data dictionary logging path which is only used by one set of tests).

Co-authored by @piekstra

Author Checklist

  • Unit tests, Integration tests, and Unbounded tests completed
  • Performance testing completed with satisfactory results (if required)

Reviewer Checklist

  • Perform code review
  • Pull request was adequately tested (new/existing tests, performance tests)

piekstra and others added 6 commits March 12, 2026 13:51
#3470)

* feat: Extract structured log message arguments as context data for MEL

When using Microsoft.Extensions.Logging with structured logging
(e.g., logger.LogInformation("User {UserId} did {Action}", userId, action)),
the named parameters are now extracted from the TState state parameter and
merged into context data. This makes them available as individual log
attributes in New Relic (e.g., context.UserId, context.Action), matching
the behavior already available for Serilog and NLog.

The state extraction is wrapped in its own try/catch to ensure it cannot
disrupt existing scope-based context data harvesting. The {OriginalFormat}
key (the message template string) is excluded.

Fixes #2519

* test: Add integration test for MEL structured log arg context data

Adds StructuredLogArgContextDataTests that verifies structured log
message arguments (e.g., {Name}, {Id}) are extracted as context data
attributes when using Microsoft.Extensions.Logging.

- Adds InfoWithStructuredArgs to MicrosoftLoggingLoggingAdapter
- Adds CreateSingleLogMessageWithStructuredArgs to LoggingTester
- Test asserts that Name and Id appear in the log line attributes
…ub.com:newrelic/newrelic-dotnet-agent into feature/mel-structured-logging-as-context-data
@nr-ahemsath nr-ahemsath marked this pull request as ready for review March 26, 2026 16:04
@nr-ahemsath nr-ahemsath requested a review from a team as a code owner March 26, 2026 16:04
@nr-ahemsath nr-ahemsath marked this pull request as draft March 26, 2026 17:48
Separate the context data handling testing from the main message logging paths
@nr-ahemsath nr-ahemsath marked this pull request as ready for review March 26, 2026 21:57
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.77%. Comparing base (871f063) to head (e5089b0).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3479      +/-   ##
==========================================
- Coverage   81.79%   81.77%   -0.02%     
==========================================
  Files         508      508              
  Lines       34220    34220              
  Branches     4040     4040              
==========================================
- Hits        27990    27984       -6     
- Misses       5265     5269       +4     
- Partials      965      967       +2     
Flag Coverage Δ
Agent 82.76% <ø> (-0.02%) ⬇️
Profiler 71.75% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 1 file 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.

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.

MicrosoftExtensionsLogging: Add each log argument/attribute to the context data

4 participants