I am using Serilog for logging and cant' figure out how to separate log events to different files. For example, I want to log errors to error_log-ddmmyyyy.txt and warnings to warn_log-ddmmyyyy.txt.
Here goes my logger configuration:
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Level", "Warning"))
.WriteTo.RollingFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs\warn_log-{Date}.txt"),
outputTemplate: OutputTemplate))
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Level", "Error"))
.WriteTo.RollingFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs\error_log-{Date}.txt"),
outputTemplate: OutputTemplate))
.CreateLogger();
It only works when I specify {Level} property exatcly in log message.
I was trying to use:
Matching.WithProperty<LogEventLevel>("Level", l => l == LogEventLevel.Warning)
but it didn't work too.
Another variant is to have ERROR and FATAL in one file (instead of errors in one file and fatal in another). Seems to many files for me having them separate. Just to keep in mind "or" operator can be used.
These are my nuget packages:
I use the following configuration and it works for me:
I think you need:
Edit:
In many cases it's now more succinct to use Serilog.Sinks.Map. With it, the example can be written as: