Prasad Kanaparthi
Prasad Kanaparthi

Reputation: 6563

Configuring Serilog File with web.config

I'm trying to configure Serilog for a .NET FW 4.7.1 project. Here's what I have in my web.config:

Problem: serilog:write-to:File.outputTemplate is not working

<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. Not logging AppName as well.

Upvotes: 3

Views: 3891

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31832

It's not possible to mix the XML configuration with the WriteTo.File() in code.

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().

Upvotes: 3

Related Questions