Honor IncludeCategory in the JSON log format#2474
Open
TemRevil wants to merge 2 commits into
Open
Conversation
In text format the ILogger category is written on every line when LambdaLoggerOptions.IncludeCategory is true (the default), but the JSON branch of LambdaILogger.Log never emitted the category, so migrating a function from text to JSON logging silently lost it with no way to opt back in (aws#2469). When IncludeCategory is set, prepend a {Category} placeholder to the message template and supply the category value, so it surfaces as a queryable property in the JSON record instead of being dropped. Text format is unchanged, and JSON output is unchanged when IncludeCategory is false. Fixes aws#2469
Contributor
There was a problem hiding this comment.
Pull request overview
Updates Amazon.Lambda.Logging.AspNetCore to honor LambdaLoggerOptions.IncludeCategory when Lambda JSON logging is enabled, so category information isn’t silently dropped when switching from text to JSON logs.
Changes:
- Prepend a
{Category}placeholder (and corresponding argument) to the JSON logging message template whenIncludeCategoryis enabled. - Add tests asserting JSON parameter logging includes/excludes category depending on
IncludeCategory. - Add an autover changelog entry describing the fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs | Adds category injection in the JSON logging branch when IncludeCategory is enabled. |
| Libraries/test/Amazon.Lambda.Logging.AspNetCore.Tests/LoggingTests.cs | Adds tests validating category presence/absence impacts parameter count in JSON mode. |
| .autover/changes/541b1775-ecf8-4e75-99c8-675924a5c34e.json | Records a patch changelog note for the logging fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+78
to
+86
| if (_options.IncludeCategory) | ||
| { | ||
| // Unlike the text format, the JSON format otherwise drops the | ||
| // category entirely, so IncludeCategory has no effect. Prepend a | ||
| // "{Category}" placeholder (which the JSON formatter turns into a | ||
| // queryable property) and supply the category value. | ||
| messageTemplate = "[{Category}] " + messageTemplate; | ||
| parameters.Insert(0, _categoryName); | ||
| } |
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.
Fixes #2469
Problem
With
Amazon.Lambda.Logging.AspNetCore, theILoggercategory is written on every line in text format whenLambdaLoggerOptions.IncludeCategoryis true (which is the default). The JSON branch ofLambdaILogger.Log, however, only forwards the message template and its arguments and never emits the category, soIncludeCategoryis silently ignored in JSON mode. Migrating a function from text to JSON logging drops the category entirely, with no way to opt back in.Change
When
IncludeCategoryis set, the JSON branch now prepends a{Category}placeholder to the message template and supplies the category as the matching argument. Because the JSON formatter turns named template parameters into top-level properties, the category surfaces as a queryable property (and in the rendered message), matching the parity the text format already provides.IncludeCategory = false: unchanged (no extra property, same parameter count).IncludeCategory = true: the category is included.This follows the reporter's suggested "at minimum honor IncludeCategory" approach and needs no runtime-side change. If the maintainers would rather expose it as a dedicated top-level
categoryfield emitted byJsonLogMessageFormatter(the issue's preferred option), I'm happy to take that direction instead.Testing
Amazon.Lambda.Logging.AspNetCore.Testson net10.0:Passed! Failed: 0, Passed: 16. AddedTestJSONParameterLoggingIncludesCategoryWhenEnabled(category present, parameter count goes 3 to 4) andTestJSONParameterLoggingOmitsCategoryWhenDisabled(no category, count stays 3); the existingTestJSONParameterLoggingstill passes since it usesIncludeCategory = false.