I'm trying to use serilog's ExpressionTemplate
and write the log level in the following way:
{ "level": "INF", ... }
I know how to alias @l
to level - level: @l
and I know how to format the level to three upper-case letters - @l:u3
but I'm unable to combine the two
I have tried the following without success (fails when trying to format the template with an exception):
level: @l:u3
{'level': @l:u3}
At the time of writing this is a limitation in Serilog.Expressions, which will hopefully be addressed soon.
Update: version 3.4.0-dev-* now on NuGet supports
ToString(@l, 'u3')
.
You can work around it with conditionals:
{'level': if @l = 'Information' then 'INF' else if @l = 'Warning' then 'WRN' else 'ERR'}
With a few more branches for remaining levels.
(Alternatively, you could write and plug in a user defined function along the lines of ToShortLevel(@l)
, and use that instead.)