When using Serilog with .NET, I want to use a different output template for error level logs, since I want the error logs to contain additional information. I'm using the appsettings.json to configure how Serilog behaves, so I need a solution which uses JSON instead of C#.
I am able to use "restrictiedToMinimumLevel" : "Error" to produce error logs with the additional information I want, but this results in the logs appearing twice (once with the extra information, and once without). I've also found a way to do exactly what I want using Serilog.Filters.Expressions, but that package was deprecated several years ago. There's a new package, Serilog.Expressions, but the syntax appears to have changed, because my application fails to start using the old syntax.
Here is what I've tried using with Serilog.Expressions:
{
"Serilog": {
"Using": [ "Serilog.Expressions" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Error",
"outputTemplate": "This should only log errors: {Message} {Exception}"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@Level != 'Error'"
}
}
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "This should log anything that isn't an error: {Message} {Exception}"
}
}
]
}
}
}
]
}
}
How can I use different output templates for different log levels when using Serilog?