Serilog - multiple log files
I use the following configuration and it works for me:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information).WriteTo.RollingFile(@"Logs\Info-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug ).WriteTo.RollingFile(@"Logs\Debug-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning ).WriteTo.RollingFile(@"Logs\Warning-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error ).WriteTo.RollingFile(@"Logs\Error-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Fatal ).WriteTo.RollingFile(@"Logs\Fatal-{Date}.log"))
.WriteTo.RollingFile(@"Logs\Verbose-{Date}.log")
.CreateLogger();
I think you need:
.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
Edit:
In many cases it's now more succinct to use Serilog.Sinks.Map. With it, the example can be written as:
Log.Logger = new LoggerConfiuration()
.WriteTo.Map(
evt => evt.Level,
(level, wt) => wt.RollingFile("Logs\\" + level + "-{Date}.log"))
.CreateLogger();
Usually for my use-cases I need multiple logs that will contain a minimum level for each. This way I can later investigate the logs more efficiently.
For more recent versions of Serilog I would suggest to use this:
Log.Logger = new LoggerConfiuration()
.MinimumLevel.Debug()
.WriteTo.File(path: "debug.log", rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "info.log", restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "error.log",restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning)
.CreateLogger();