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
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();
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.
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs\\all.log", rollingInterval: RollingInterval.Day)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(y => y.Level == Serilog.Events.LogEventLevel.Error || y.Level == Serilog.Events.LogEventLevel.Fatal)
.WriteTo.File("logs\\error.log", rollingInterval: RollingInterval.Day))
.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();
var dateTimeNowString = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.File($"Logs/{dateTimeNowString}-Error.log")
)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
.WriteTo.File($"Logs/{dateTimeNowString}-Warning.log")
)
.WriteTo.File($"Logs/{dateTimeNowString}-All.log")
.WriteTo.Console()
.CreateLogger();
These are my nuget packages:
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />