dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.42k stars 4.76k forks source link

Add camelCase option to the JSON console log formatter #109726

Open esbenbjerre opened 1 week ago

esbenbjerre commented 1 week ago

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

dotnet-policy-service[bot] commented 1 week ago

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 1 week ago

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