I have an application which uses Serilog to write log-data to a file. I'm now trying to configure my logger with sub-loggers in appsettings.json in such a way so that I can filter certain logs to a different file. I'm aware that I could configure the logger in code, but I want to do it via appsettings.json.
Basically, I want all logs to go to the same file, except a certain type of log. I used the Serilog wiki and a few blog posts and stackoverflow entries, mainly this one, to achieve my goal.From what I've read using the following should allow me to filter for this type of log entry:
using (LogContext.PushProperty("SpecialLogType", true)) {
_logger.LogInformation("MyLogEntry {MyParam}", myParam);
}
I configured two sinks, one for normal logs and one for this special type of log. Using filters, I should now be able to filter logs using this property. But I can't figure out how exactly I need to configure the subloggers in appsettings.json.
Right now, my appsettings.json looks like this:
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions" ],
"MinimumLevel": {
"Default": "Information",
},
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@p['SpecialLogType'] = 'true'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/NormalTypeLog_.txt",
// other configuration
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@p['SpecialLogType'] = 'true'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/SpecialTypeLog.json",
// other configuration
}
}
]
}
}
}
]
}
I've tried several different things with some results but I can't get it to work properly and would appreachiate some advice. Does anyone have any tipps?