Currently I have all configuration written in code. I use subloggers for filtering and changing the storage of logging. Is there any way to do it from configuration file. Because I want to have a separate config file for every context in solution.
Asked
Active
Viewed 2,158 times
1
-
This might be useful, [question]: http://stackoverflow.com/a/41989190/7038900 – Spharah Feb 01 '17 at 20:40
2 Answers
0
If the number of sub-loggers is fixed at build time, you can use configuration prefixes to do this:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings() // default file
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other1.config"))
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other2.config"))
.CreateLogger();
There's no support in Serilog.Settings.AppSettings yet, but in theory there's nothing preventing it being added if someone's able to implement it.

Nicholas Blumhardt
- 30,271
- 4
- 90
- 101
0
Try this
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"/>

Spharah
- 625
- 7
- 18
-
Please don't add [the same answer](http://stackoverflow.com/a/41989190/4687348) to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](http://meta.stackexchange.com/q/104227/347985) – FelixSFD Feb 03 '17 at 08:53