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

Allow customization of 'LogLevel' text output #1510

Closed
julealgon opened this issue Dec 3, 2020 · 4 comments
Closed

Allow customization of 'LogLevel' text output #1510

julealgon opened this issue Dec 3, 2020 · 4 comments

Comments

@julealgon
Copy link

Is your feature request related to a problem? Please describe.
I have a hard requirement that log levels emitted by the application must be one of:

  • DEBUG
  • ERROR
  • INFO
  • WARN

Not only semantically, but the text needs to match precisely as well. This is because I'm writing to a log aggregator that is being used by several other applications using different languages and frameworks, and they have standardized their levels on these strings.

We want our logs to be able to be filtered and analyzed using existing tools and queries, which rely on those values.

Describe the solution you'd like
Expose a mechanism for manipulating the text generated from the LogEvent enumeration. The values should be completely customizable and changeable.

Ideally, one would be able to inject some sort of IFormatter<LogLevel> that translates the enum values into text, and is leveraged inside for all handling of the enum (no matter the sink or text/json formatter being used).

Describe alternatives you've considered
I can create an enricher that overrides the "level" property on each event by inspecting the LogEvent.Level property. While this works, it feels less than ideal and more of a hack, since this is not really "enriching" the log output, it is using the mechanism to perform something else entirely.

@nblumhardt
Copy link
Member

Hi Juliano!

Serilog.Expressions implements this as you would prefer.

The cleanest way to do it is to implement at MapLevel() user-defined function:

https://github.com/serilog/serilog-expressions#implementing-user-defined-functions

and the use MapLevel() in your output template.

Something like:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console(new ExpressionTemplate(
        "[{@t:HH:mm:ss} {MapLevel(@l)} ({SourceContext})] {@m}\n{@x}"))
    .CreateLogger();

Would be cool to hear how you go with it - so far there aren't a lot of examples out there for Serilog.Expressions (it's brand new) but I have high hopes it'll become a core part of the Serilog toolkit, especially for customizing JSON and text output.

HTH!

@julealgon
Copy link
Author

Hi Nick

Serilog.Expressions implements this as you would prefer.

Wow! First time I'm hearing of this extension. Thanks for pointing it out to me.

That seems pretty insane! You guys are crazy for coming up with a brand new DSL for that, kudos!

and the use MapLevel() in your output template.

That seems to work extremely well for text-based logging, but the problem is that I'm currently using compact json form. My understanding from quickly reading what Expressions allows, is that it currently only supports flows using ITextFormatter.

It sounds to me like a hook is missing somewhere between the conversion of C# LogEvent to the actual properties being written. Ideally, a solution there would work for any "output" format, be it text, or json.

@nblumhardt
Copy link
Member

Already a step ahead, @julealgon! :-) Serilog.Expressions was designed to fully-support JSON:

    .WriteTo.Console(new ExpressionTemplate(
        "{ {@t, @mt, @l: MapLevel(@l), @x, ..@p} }\n"))

https://github.com/serilog/serilog-expressions#formatting

@julealgon
Copy link
Author

Ohhhh so you completely bypass the Json formatter?! That's indeed very interesting Nick... I actually think this will be very useful for me for other reasons as well, now that I think about it. This combines the best of both worlds really.

My mistake for not reading the entire readme and completely missing the Json examples!

This expressions package will be a lot more fun than I anticipated.

Closing this as it now fully solves the problem I proposed.

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