1

I am using serilog in a web api in .net framework with the serilog config added in the Web.Config file as below

 <appSettings>
        <add key="serilog:minimum-level" value="Information" />
        <add key="serilog:using:File" value="Serilog.Sinks.File" />
        <add key="serilog:write-to:File.path" value="C:\api\logs\api_log.log" />
        <add key="serilog:write-to:File.formatter" value="Serilog.Formatting.Json.JsonFormatter, Serilog" />
        <add key="serilog:write-to:File.rollingInterval" value="Day" />
        <add key="serilog:write-to:File.fileSizeLimitBytes" value="5242880" />
        <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
        <add key="serilog:write-to:File.shared" value="true" />
    </appSettings>

I have registered the logger in the startup file as below

private static ILogger ConfigureSerilog()
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                                               .Add(new XmlConfigurationProvider())
                                               .Build();

            LoggerConfiguration loggerConfiguration = new LoggerConfiguration().Enrich.With(new SerilogClaimValueEnricher())
                                                                               .Enrich.WithDemystifiedStackTraces()
                                                                               .ReadFrom.Configuration(configuration);

            Logger rootLogger = loggerConfiguration.CreateLogger();
            Log.Logger = rootLogger;

            return rootLogger;
        }

The configuration object is getting all the values that we are given in the web.config but the logging is not working. But instead of ReadFrom.Configuration(configuration) if I use ReadFrom.AppSettings() the logging is working corectly. But I need make use of IConfigurationRoot here. Did I missed anything?

Please not XmlConfigurationProvider is a custom class to read keys from web.config

Below is the screenshot of debug mode states of the configuration.

Below is the debug mode states of the configuration

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
Midhun P
  • 126
  • 1
  • 11
  • Don't you need to provide a path to `new XmlConfigurationProvider()` ? how does the configuration builder know what file to load? – sommmen Jan 18 '22 at 09:40
  • it is currently fetching values from the web.config file and that is working fine. Only logging is not happening – Midhun P Jan 18 '22 at 09:43
  • 1
    Please format you code so we can read it: please read [this](https://meta.stackoverflow.com/a/251362) – JHBonarius Jan 18 '22 at 10:21

0 Answers0