I'm trying to create an global output template for serilog messages that follows the following format:
{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [{ComponentName}, {ApplicationName}, {ThreadId}] {Level} ({ErrorId}): {Message} {Exception}
The trouble I'm running into is that, some messages will not contain error IDs, or exceptions, or ThreadIds. So, when this case occurs, I get a message that has a bunch of characters that add noise to the log message, e.g.,
2015-06-24 15:11:03,234 [Component, MyApp, ] Info (): This is a message that I'm writing
Is it possible to have Serilog support optional parameters in the message template?
You can use
Serilog.Expressions
to control the output using anExpressionTemplate
with conditional blocks so that only properties that are present are written to the output, including spaces and separators. e.g.