Add JsonNamingPolicy support to JsonConsoleFormatterOptions#125692
Closed
piekstra wants to merge 1 commit intodotnet:mainfrom
Closed
Add JsonNamingPolicy support to JsonConsoleFormatterOptions#125692piekstra wants to merge 1 commit intodotnet:mainfrom
piekstra wants to merge 1 commit intodotnet:mainfrom
Conversation
Author
|
@dotnet-policy-service agree |
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-logging |
Adds a JsonNamingPolicy property to JsonConsoleFormatterOptions that controls the casing of built-in property names (Timestamp, EventId, LogLevel, Category, Message, Exception, State, Scopes). When null (default), the existing PascalCase names are preserved. When set to e.g. JsonNamingPolicy.CamelCase, the output uses camelCase names (timestamp, eventId, logLevel, etc.). Fixes dotnet#125689
99c034c to
8690657
Compare
This was referenced Mar 18, 2026
Contributor
|
As the #125689 adds new public API, it needs to go through API review process. I'll close this for now. We can reopen and continue when/if the API is approved. |
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 #125689
Summary
Adds a
JsonNamingPolicyproperty toJsonConsoleFormatterOptionsthat controls the casing of built-in JSON property names emitted byJsonConsoleFormatter.When
null(default): PascalCase names are preserved — no behavioral change.When set (e.g.
JsonNamingPolicy.CamelCase): Built-in property names are transformed:TimestamptimestampEventIdeventIdLogLevellogLevelCategorycategoryMessagemessageExceptionexceptionStatestateScopesscopesUser-provided property names from structured log state and scope data are written as-is.
Usage
Motivation
Observability platforms (New Relic, Datadog, Splunk, Grafana Loki) expect lowercase property names in structured JSON logs. The current
JsonConsoleFormatterhardcodes PascalCase names with no way to change them, forcing users to write a customConsoleFormatterfrom scratch (~100 lines of boilerplate). This has been requested multiple times (#109726, dotnet/aspnetcore#58890).Implementation notes
Property names are pre-computed in
ReloadLoggerOptions(which already runs on construction and option reload) rather than callingJsonNamingPolicy.ConvertName()per log entry. This avoids per-entry overhead, though for built-in policies likeCamelCasethe cost would be negligible either way. The pre-computation approach was chosen becauseReloadLoggerOptionsis the existing pattern for applying option changes, so it's the natural place to derive the names — and it means the hot path (WriteInternal) stays allocation-free with just field reads.Tests
Three new tests in
JsonConsoleFormatterTests.cs:Log_JsonNamingPolicyCamelCase_ProducesCamelCasePropertyNames— verifies all built-in properties use camelCase and PascalCase names are absentLog_JsonNamingPolicyNull_PreservesPascalCasePropertyNames— verifies default behavior is unchangedLog_JsonNamingPolicyCamelCase_ScopePropertyNamesAreCamelCase— verifies scope array and innerMessagekey also use the naming policy