Anton Kiprianov Anton Kiprianov - 2 months ago 50
C# Question

Serilog - multiple log files

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.

Answer

I think you need:

.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)