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.
By default, serilog will only log to the console.
Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.
Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.
Changing the log event level. Serilog defines several levels of log events. From low to high, these are Verbose , Debug , Information , Warning , Error and Fatal . You can set the minimum level you want to log, meaning that events for that level or higher will be logged.
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();
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