Skip to content

Add camelCase option to the JSON console log formatter #109726

@esbenbjerre

Description

@esbenbjerre

Background and motivation

When the JSON console log formatter is used properties are formatted with PascalCase

{"EventId":15,"LogLevel":"Warning","Category":"Microsoft.AspNetCore.Hosting.Diagnostics","Message":"Overriding HTTP_PORTS..."},"Scopes":[]}

I would like an option to use camelCase instead

{"eventId":15,"logLevel":"Warning","category":"Microsoft.AspNetCore.Hosting.Diagnostics","message":"Overriding HTTP_PORTS..."},"scopes":[]}

API Proposal

Add a NamingPolicy property to JsonWriterOptions.

API Usage

using System.Text.Json;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddJsonConsole(options =>
{
    options.IncludeScopes = false;
    options.TimestampFormat = "HH:mm:ss ";
    options.JsonWriterOptions = new JsonWriterOptions
    {
        Indented = true
        NamingPolicy = JsonNamingPolicy.CamelCase;
    };
});

}

or

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning"
    },
    "Console": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning"
      },
      "FormatterName": "json",
      "FormatterOptions": {
        "SingleLine": true,
        "IncludeScopes": true,
        "NamingPolicy": "CamelCase"
      }
    }
  }

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions