Skip to content

JsonConsoleFormatter: Add JsonNamingPolicy support for built-in property names #125689

@piekstra

Description

@piekstra

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions