feat: Include structured log message arguments as context data for MEL#3479
Open
nr-ahemsath wants to merge 12 commits intomainfrom
Open
feat: Include structured log message arguments as context data for MEL#3479nr-ahemsath wants to merge 12 commits intomainfrom
nr-ahemsath wants to merge 12 commits intomainfrom
Conversation
#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
Separate the context data handling testing from the main message logging paths
tippmar-nr
reviewed
Mar 31, 2026
...lic/Agent/Extensions/Providers/Wrapper/MicrosoftExtensionsLogging/MicrosoftLoggingWrapper.cs
Outdated
Show resolved
Hide resolved
tippmar-nr
reviewed
Mar 31, 2026
tests/Agent/IntegrationTests/IntegrationTests/Logging/StructuredLogArgContextDataTests.cs
Show resolved
Hide resolved
tippmar-nr
reviewed
Mar 31, 2026
...n/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/DummyMELAdapter.cs
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Log4Netis 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
Reviewer Checklist