Description
Description
When logging to the console using the JSON formatter, log messages are often duplicated: once as the top-level Message
, again within the State
object, and a third time as the original format string. This redundancy typically arises in common usage scenarios, such as with logger.LogInformation(...)
.
The proposed breaking change addresses this by eliminating the duplicated message entries in the logs.
The change is done in .NET 10 Preview 7.
Version
Other (please put exact version in description textbox)
Previous behavior
Consider the following simple example, where logger
is a console logger configured with the JSON formatter:
logger.LogInformation("This is an information message.");
Previously, this code would produce output like:
{
"EventId": 0,
"LogLevel": "Information",
"Category": "Program",
"Message": "This is an information message.",
"State": {
"Message": "This is an information message.",
"{OriginalFormat}": "This is an information message."
}
}
As you can see, Message
appears twice— once as the top-level Message
, again inside the State
object, leading to unnecessary duplication in the log output.
New behavior
After the change, the log output will look like this:
{
"EventId": 0,
"LogLevel": "Information",
"Category": "Program",
"Message": "This is an information message.",
"State": {
"{OriginalFormat}": "This is an information message."
}
}
The Message
is no longer duplicated—it now appears only at the top level, resulting in cleaner and more concise log output.
Type of breaking change
- Binary incompatible: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
- Behavioral change: Existing binaries might behave differently at run time.
Reason for change
The goal of this change is to reduce unnecessary logging overhead by eliminating duplicate content. By avoiding repeated formatting of the same message, the change helps:
- Minimize log output size
- Reduce confusion caused by redundant information
- Improve performance by preventing multiple formatting operations for the same message
Overall, this results in cleaner, more efficient, and easier-to-read logs.
Recommended action
For users who were previously parsing the logging output and extracting the Message
from within the State
object, it's safe to use the top-level Message
instead, now that duplication has been removed.
Note: In some cases, a Message
may still appear within the State
object—this typically happens when its content differs from the top-level Message
.
Feature area
Extensions
Affected APIs
The Microsoft.Extensions.Logging APIs, particularly the Console Logger Extensions, available as part of the Microsoft.Extensions.Logging.Console NuGet package.
Metadata
Metadata
Assignees
Type
Projects
Status