-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Description
JsonConsoleFormatter hardcodes PascalCase property names (Timestamp, EventId, LogLevel, Category, Message, Exception, State) with no option to configure casing. This is a significant gap for users who forward structured JSON logs to observability platforms (New Relic, Datadog, Splunk, Grafana Loki, etc.) that expect lowercase or camelCase property names.
For example, New Relic's CloudWatch log forwarder looks for message (lowercase) to populate the log message field, and level for severity. With the current JsonConsoleFormatter, these fields are Message and LogLevel — causing log queries by message or level to return no results.
The only workaround today is writing a custom ConsoleFormatter from scratch, which multiple people have noted is frustrating for what should be a configuration option:
- Duplicate information in structured json logs #104409 — "One could say I can write a logger, but is it really the purpose of reinventing the wheel ... again?"
- Enhance JsonConsoleFormatter to be configurable and avoid to duplicate information #110373 — "custom provider but highly undesired"
Proposed API
Add a JsonNamingPolicy property to JsonConsoleFormatterOptions:
public class JsonConsoleFormatterOptions : ConsoleFormatterOptions
{
// Existing properties...
/// <summary>
/// Gets or sets the naming policy for built-in JSON property names
/// (Timestamp, EventId, LogLevel, Category, Message, Exception, State).
/// When null (default), PascalCase is used for backward compatibility.
/// </summary>
public JsonNamingPolicy? JsonNamingPolicy { get; set; }
}Usage:
builder.Logging.AddJsonConsole(options =>
{
// camelCase output: timestamp, eventId, logLevel, category, message, exception, state
options.JsonNamingPolicy = JsonNamingPolicy.CamelCase;
});Impact
This affects any .NET application using AddJsonConsole() with CloudWatch, New Relic, Datadog, Splunk, or any observability platform that expects lowercase/camelCase JSON log properties. The current workaround (custom ConsoleFormatter) requires ~100 lines of boilerplate that reimplements most of JsonConsoleFormatter.
Implementation notes
The change would be in JsonConsoleFormatter.cs, applying the naming policy to the hardcoded property name strings in the Write method. The default (null) preserves current behavior for backward compatibility.