Avoid duplicate messages in Console logging with Json formatter #117033
+25
−16
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 #104409
When logging to the console using the JSON formatter, the log message often appears redundantly—once as the top-level
Message
, again inside theState
object, and a third time as the original format string. This commonly occurs in typical usage patterns likelogger.LogInformation(...)
.This duplication:
Example
Current output:
After the change
In addition to removing the redundant
Message
field fromState
, I've optimizedFormattedLogValues.ToString()
to avoid reformatting the message multiple times. This method is frequently called more than once in common scenarios (e.g.,LogInformation
), resulting in wasted processing. The optimization ensures formatting occurs only once.Finally, I plan to file a breaking change issue, as this adjustment may affect consumers who rely on the exact shape of the JSON output for parsing or processing.