Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to configure multiple Serilog RollingFiles through appSetting configuration

Tags:

serilog

Is there a way to configure multiple Serilog RollingFiles through appSetting?

I want to create separate log files for Information and Error levels.

like image 560
wali Avatar asked Feb 10 '16 12:02

wali


2 Answers

Configuring multiple logs is pretty simple in appsettings, mission is to use filter. i spent almost 3hours trying to figure out how to archieve this.

I ended up doing the following:

Startup.cs / Global.asax.cs

Log.Logger = new LoggerConfiguration()
           .WriteTo
           .Logger(x => x.Filter
           .ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Error)
           .ReadFrom
           .AppSettings("error"))

           .WriteTo
           .Logger(x => x.Filter
           .ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Information)
           .ReadFrom
           .AppSettings("info")).CreateLogger()

Web.Config

<add key ="error:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="error:serilog:write-to:RollingFile.pathFormat" value="C:\log\error {Date}.txt"/>
<add key ="error:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>

<add key ="info:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="info:serilog:write-to:RollingFile.pathFormat" value="C:\log\info {Date}.txt"/>
<add key ="info:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>
like image 60
Spharah Avatar answered Oct 30 '22 11:10

Spharah


Not directly - it's possible to use a setting prefix like:

.ReadFrom.AppSettings()
.ReadFrom.AppSettings(settingPrefix: "2")

And then add the additional sink like:

<add key="2:serilog:write-to:RollingFile.pathFormat" value="..." />

Baking this properly into the app settings configuration provider has been a "TODO" for a while.

If configuring the sinks in code is possible, that's probably the way to go.

like image 28
Nicholas Blumhardt Avatar answered Oct 30 '22 11:10

Nicholas Blumhardt