簡體   English   中英

使用 web.config 配置 Serilog 文件

[英]Configuring Serilog File with web.config

我正在嘗試為 .NET FW 4.7.1 項目配置 Serilog。 這是我的 web.config 中的內容:

問題serilog:write-to:File.outputTemplate不起作用

<appSettings>
    <add key="serilog:minimum-level" value="Debug" />

    <add key="serilog:using:File" value="Serilog.Sinks.File" />
    <!--<add key="serilog:write-to:File.path" value="C:\Logs" />--> (This will be dynamically generate, Per User)
    <add key="serilog:write-to:File.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] | ({**AppName**}) | {Message:lj}{NewLine}{Exception}" />
    <add key="serilog:write-to:File.shared" value="true" />
    <add key="serilog:write-to:File.rollingInterval" value="Day"/>

    <add key="serilog:enrich:with-property:AppName" value="MyApp" />
</appSettings>

在代碼中,

 var userlogPath =  Path.Combine("C:\Logs", $"User_{UserId}_.log"); // Logged in **UserId** will be passed.

 var logger = Log.Logger = new LoggerConfiguration()
      .ReadFrom.AppSettings()
      .WriteTo.File(userlogPath)
      .CreateLogger();

logger.Information("Test Log"); // 它不考慮來自 Web.Config 的outputTemplate 也不記錄AppName

不可能在代碼中混合 XML 配置和WriteTo.File()

File.path需要在 XML 中指定(如您所示,已注釋掉),或者需要將File.outputTemplate等其他設置提供給WriteTo.File()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM