'Serilog ExpressionTemplate rename log level and apply formatting
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}
Solution 1:[1]
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.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
