Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json output can result in strange log messages #2003

Open
dnperfors opened this issue Jan 25, 2024 · 1 comment
Open

Json output can result in strange log messages #2003

dnperfors opened this issue Jan 25, 2024 · 1 comment

Comments

@dnperfors
Copy link

Description
In our web application we use new JsonFormatter(null, renderMessage: true) to format our messages to the console as JSON, including the RenderMessage.
In ASP.NET Core 7 this resulted in messages like the following:

{
  "Timestamp":"2024-01-25T11:03:59.4243414+01:00",
  "Level":"Information",
  "MessageTemplate":"{HostingRequestStartingLog:l}",
  "RenderedMessage":"Request starting HTTP/1.1 GET http://localhost:8080/v1/health - -",
  "Properties": {
    "Protocol":"HTTP/1.1",
    "Method":"GET",
    "ContentType":null,
    "ContentLength":null,
    "Scheme":"http",
    "Host":"localhost:8080",
    "PathBase":"",
    "Path":"/v1/health",
    "QueryString":"",
    "HostingRequestStartingLog":"Request starting HTTP/1.1 GET http://localhost:8080/v1/health - -",
    "SourceContext":"Microsoft.AspNetCore.Hosting.Diagnostics",
    "RequestPath":"/v1/health"
  },
  "Renderings": { "HostingRequestStartingLog": [ { "Format":"l", "Rendering":"Request starting HTTP/1.1 GET http://localhost:8080/v1/health - -" } ] }
}

In ASP.NET Core 8, they changed the message template of the log entry and now it shows a bit differently:

{
  "Timestamp":"2024-01-18T12:20:03.6249006+01:00",
  "Level":"Information",
  "MessageTemplate":"Request starting {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {ContentType} {ContentLength}",
  "RenderedMessage":"Request starting \"HTTP/1.1\" \"GET\" \"http\"://\"localhost:8080\"\"\"\"/v1/health\"\"\" - null null",
  "Properties": {
    "Protocol": "HTTP/1.1",
    "Method": "GET",
    "ContentType": null,
    "ContentLength": null,
    "Scheme": "http",
    "Host": "localhost:8080",
    "PathBase": "",
    "Path": "/v1/health",
    "QueryString": "",
    "EventId": {"Id":1},
    "SourceContext":"Microsoft.AspNetCore.Hosting.Diagnostics",
    "RequestPath":"/v1/health"
  }
}

Note the quotes in the URL.
Is there a way to configure the MessageTemplateRenderer to use literal values instead of quoted json strings?

Relevant package, tooling and runtime versions
Serilog 3.1.1 via Serilog.AspNetCore 7.0.0

Additional context
Add any other context about the problem here.

@dnperfors dnperfors added the bug label Jan 25, 2024
@nblumhardt
Copy link
Member

Thanks for the note. This is a change in ASP.NET Core, rather than Serilog - Serilog has always quoted strings in the default message rendering mode.

I'd love to change this - I don't think it'd be out of the question for a Serilog 4.0 - but for now your best bet is to modify the formatter.

One simple, quick solution is to switch to Serilog.Expressions for JSON formatting:

    .WriteTo.Console(new ExpressionTemplate(
        "{ {Timestamp: @t, Level: @l, MessageTemplate: @mt, RenderedMessage: @m, Exception: @x, Properties: @p} }\n"))

Serilog.Expressions already uses the nicer message rendering that you're after.

If the code is performance-sensitive at this level, you might instead grab the code for MessageTemplateRenderer from:

https://github.com/serilog/serilog/blob/dev/src/Serilog/Rendering/MessageTemplateRenderer.cs

and then embed a modified version of RenderedCompactJsonFormatter that uses it (with slightly modified structure and field names) into your project:

https://github.com/serilog/serilog-formatting-compact/blob/dev/src/Serilog.Formatting.Compact/Formatting/Compact/RenderedCompactJsonFormatter.cs

HTH,
Nick

@nblumhardt nblumhardt added discussion and removed bug labels Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants