2016-02-10 175 views

回答

3

不能直接 - 它可以使用設置前綴,如:

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

然後添加額外的水槽,如:

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

烘烤這個正確到應用程序設置的配置提供商已經「TODO」一會兒。

如果在代碼中配置接收器是可能的,那可能就是要走的路。

+0

謝謝您的回答。儘管你指出了我正確的方向,但我試過了但沒有奏效。 我不得不看一下代碼,找出爲什麼它沒有拿起配置。代碼是: '_settingPrefix = settingPrefix == null? 「serilog:」:string.Format(「{0}:serilog:」,settingPrefix); ' 所以我必須提供如下前綴: wali

+0

在上面的註釋中,「custom」是前綴在API中使用即。 '.ReadFrom.AppSettings(settingPrefix:「custom」)' – wali

+0

謝謝 - 修正了這個例子。 –

3

配置多個日誌在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"/> 
相關問題