Is there a way to configure multiple Serilog RollingFiles through appSetting?
I want to create separate log files for Information and Error levels.
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"/>
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.
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