Skip to content

Avoid duplicate messages in Console logging with Json formatter #117033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Jun 25, 2025

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 the State object, and a third time as the original format string. This commonly occurs in typical usage patterns like logger.LogInformation(...).

This duplication:

  • Increases log size
  • Causes confusion due to repeated content
  • Adds unnecessary overhead by formatting the same message multiple times

Example

logger.LogInformation("This is an information message.");

Current output:

{
  "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."
  }
}

After the change

{
  "EventId": 0,
  "LogLevel": "Information",
  "Category": "Program",
  "Message": "This is an information message.",
  "State": {
    "{OriginalFormat}": "This is an information message."
  }
}

In addition to removing the redundant Message field from State, I've optimized FormattedLogValues.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.

@tarekgh tarekgh added this to the 10.0.0 milestone Jun 25, 2025
@tarekgh tarekgh added the breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. label Jun 25, 2025
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Jun 25, 2025
Copy link
Contributor

dotnet-policy-service bot commented Jun 25, 2025

Added needs-breaking-change-doc-created label because this PR has the breaking-change label.

When you commit this breaking change:

  1. Create and link to this PR and the issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.
  2. Ask a committer to mail the .NET Breaking Change Notification DL.

Tagging @dotnet/compat for awareness of the breaking change.

@tarekgh tarekgh removed the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Jun 30, 2025
@tarekgh
Copy link
Member Author

tarekgh commented Jun 30, 2025

Breaking change issue is filed dotnet/docs#47006 and email sent to the .NET Breaking Change Notification DL.

@tarekgh tarekgh merged commit baf82cb into dotnet:main Jun 30, 2025
82 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Logging breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate information in structured json logs
3 participants