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.
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!
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.
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"))
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.
Most helpful comment
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.