We would like to get log for:
All of this configured in appsettings.json.
This is the appsettings.json file for the application:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Literate", "Serilog.Sinks.File", "Serilog.Filters.Expressions" ],
"MinimumLevel": "Verbose",
"WriteTo": [
{
"Name": "LiterateConsole"
},
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\FileWithoutFilter-.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"rollingInterval": "Day"
}
},
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\UPLOADERROR-.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
},
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@Level = 'Error' and UploadError is not null"
}
}
]
}
]
}
}
But, in spite of Console and File (without filtering) is running fine, File with filtering is logging all the lines, like the file (without filter).
We are sending this log.error line in C# code:
Log.Error("Fichero con error {@UploadError}", true);
But, I have said, all lines are logged to UPLOADERROR file
Some idea what is wrong in the appsettings.file?
Regards.
The solution is using subloggers with serilog. You must then configure the sublogger with the filtering and the sink.
Careful reading of serilog documentation related to general, configuration and filters, was the trick.
{
"Serilog": {
"Using": [ "Serilog.Sinks.Literate", "Serilog.Sinks.File", "Serilog.Filters.Expressions" ],
"MinimumLevel": "Verbose",
"WriteTo": [
{
"Name": "LiterateConsole"
},
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\FileWithoutFilter-.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"rollingInterval": "Day"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@Level = 'Error' and UploadError is not null"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\UPLOADERROR-.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
]
}
}
}
]
}
}
With previous configuration all is working fine now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With