简体   繁体   English

使用 web.config 配置 Serilog 文件

[英]Configuring Serilog File with web.config

I'm trying to configure Serilog for a .NET FW 4.7.1 project.我正在尝试为 .NET FW 4.7.1 项目配置 Serilog。 Here's what I have in my web.config:这是我的 web.config 中的内容:

Problem : serilog:write-to:File.outputTemplate is not working问题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>

In Code,在代码中,

 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"); // It's not considering outputTemplate from Web.Config. // 它不考虑来自 Web.Config 的outputTemplate Not logging AppName as well.也不记录AppName

It's not possible to mix the XML configuration with the WriteTo.File() in code.不可能在代码中混合 XML 配置和WriteTo.File()

Either the File.path needs to be specified in XML (as you've shown, commented out), or, the other settings like File.outputTemplate need to be supplied to WriteTo.File() . File.path需要在 XML 中指定(如您所示,已注释掉),或者需要将File.outputTemplate等其他设置提供给WriteTo.File()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM