有沒有辦法通過appSetting配置多個Serilog RollingFiles?有沒有辦法通過appSetting配置配置多個Serilog RollingFiles
我想爲信息和錯誤級別創建單獨的日誌文件。
有沒有辦法通過appSetting配置多個Serilog RollingFiles?有沒有辦法通過appSetting配置配置多個Serilog RollingFiles
我想爲信息和錯誤級別創建單獨的日誌文件。
不能直接 - 它可以使用設置前綴,如:
.ReadFrom.AppSettings()
.ReadFrom.AppSettings(settingPrefix: "2")
然後添加額外的水槽,如:
<add key="2:serilog:write-to:RollingFile.pathFormat" value="..." />
烘烤這個正確到應用程序設置的配置提供商已經「TODO」一會兒。
如果在代碼中配置接收器是可能的,那可能就是要走的路。
配置多個日誌在appsettings中非常簡單,任務是使用過濾器。我花了將近3小時試圖弄清楚如何實現這一點。
最後我做了以下內容:
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"/>
謝謝您的回答。儘管你指出了我正確的方向,但我試過了但沒有奏效。 我不得不看一下代碼,找出爲什麼它沒有拿起配置。代碼是: '_settingPrefix = settingPrefix == null? 「serilog:」:string.Format(「{0}:serilog:」,settingPrefix); ' 所以我必須提供如下前綴: –
wali
在上面的註釋中,「custom」是前綴在API中使用即。 '.ReadFrom.AppSettings(settingPrefix:「custom」)' – wali
謝謝 - 修正了這個例子。 –