Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Anton Kiprianov Avatar asked Feb 03 '15 06:02

Anton Kiprianov


People also ask

Where does Serilog write log?

By default, serilog will only log to the console.

How do I log into Serilog?

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.

What is sink in Serilog?

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.

What is Minimumlevel in Serilog?

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.


2 Answers

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(); 
like image 51
Ben Pretorius Avatar answered Sep 19 '22 10:09

Ben Pretorius


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(); 
like image 42
Nicholas Blumhardt Avatar answered Sep 19 '22 10:09

Nicholas Blumhardt