Skip to content

Document on how to support CamelCase #86

Closed
@luddskunk

Description

@luddskunk

Is your feature request related to a problem? Please describe.
I'd like to format my JSON with camelcase properties, since I am using a backend for visualization of logs (Grafana Loki) which adheres to that standard. The possibility to do so is also mentioned in https://nblumhardt.com/2021/06/customize-serilog-json-output/

Describe the solution you'd like
Use Serilog Expression to create CamelCase keys in JSON body.

Describe alternatives you've considered
I tried creating my own custom resolver to do this, but I didn't manage to get it correctly.

new ExpressionTemplate(
    "{{ \"timestamp\": \"{UtcDateTime(@t)}\", \"message\": \"{@m}\", \"level\": \"{@l}\", \"exception\": \"{@x}\",\n" +
    " {#each name, value in @p} \"{IsCamelCase(name)}\": " +
    "{#if name = 'ExceptionDetail'}" +
    "{value:j},"+
    "{#else}"+
    "\"{value}\"," +
    "{#end}{#end} }}\n"
    , nameResolver: CamelCaseResolvers))

I think this also quite quickly became too complex from maintainability standpoint.

What I want to achieve

{
    "Timestamp": "2022-11-17T07:58:15.6253633Z",
    "Message": "An unhandled exception has occurred while executing the request.",
    "Level": "Error",
    "Exception": "Exception Text"
}

should be

{
    "timestamp": "2022-11-17T07:58:15.6253633Z",
    "message": "An unhandled exception has occurred while executing the request.",
    "level": "Error",
    "exception": "Exception Text"
}

Additional context
As discussed with @nblumhardt on Twitter, I open my thread here.

Hope to get any good insights for finding a smart solution I might've overlooked!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions