Skip to content

Add JsonNamingPolicy support to JsonConsoleFormatterOptions#125692

Closed
piekstra wants to merge 1 commit intodotnet:mainfrom
piekstra:json-console-formatter-naming-policy
Closed

Add JsonNamingPolicy support to JsonConsoleFormatterOptions#125692
piekstra wants to merge 1 commit intodotnet:mainfrom
piekstra:json-console-formatter-naming-policy

Conversation

@piekstra
Copy link

Fixes #125689

Summary

Adds a JsonNamingPolicy property to JsonConsoleFormatterOptions that controls the casing of built-in JSON property names emitted by JsonConsoleFormatter.

When null (default): PascalCase names are preserved — no behavioral change.
When set (e.g. JsonNamingPolicy.CamelCase): Built-in property names are transformed:

Default CamelCase
Timestamp timestamp
EventId eventId
LogLevel logLevel
Category category
Message message
Exception exception
State state
Scopes scopes

User-provided property names from structured log state and scope data are written as-is.

Usage

builder.Logging.AddJsonConsole(options =>
{
    options.JsonNamingPolicy = JsonNamingPolicy.CamelCase;
});

Motivation

Observability platforms (New Relic, Datadog, Splunk, Grafana Loki) expect lowercase property names in structured JSON logs. The current JsonConsoleFormatter hardcodes PascalCase names with no way to change them, forcing users to write a custom ConsoleFormatter from 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 calling JsonNamingPolicy.ConvertName() per log entry. This avoids per-entry overhead, though for built-in policies like CamelCase the cost would be negligible either way. The pre-computation approach was chosen because ReloadLoggerOptions is 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 absent
  • Log_JsonNamingPolicyNull_PreservesPascalCasePropertyNames — verifies default behavior is unchanged
  • Log_JsonNamingPolicyCamelCase_ScopePropertyNamesAreCamelCase — verifies scope array and inner Message key also use the naming policy

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 17, 2026
@piekstra
Copy link
Author

@dotnet-policy-service agree

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

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
@cincuranet
Copy link
Contributor

cincuranet commented Mar 18, 2026

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.

@cincuranet cincuranet closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Logging community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonConsoleFormatter: Add JsonNamingPolicy support for built-in property names

2 participants