5

Is there a property / method in Serilog where I could (programmatically) check the current configuration? (sinks, minimum level)

e.g. if have this config:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("log.txt")
    .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information)
    .CreateLogger();

How could I read this config later?

(In my case the config is dynamically created outside my application)

Julian
  • 33,915
  • 22
  • 119
  • 174

2 Answers2

3

No, there's no reflective way to examine Serilog configuration.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • 2
    That's unexpected! So if you like to alter the config, you need to recreate the whole config, isn't? – Julian Jul 08 '19 at 08:39
  • PS in NLog reading and changing the config could be done by `LogManager.Configuration`. So I was looking for that ;) – Julian Jul 08 '19 at 08:40
  • Yep, we do things differently :-) – Nicholas Blumhardt Jul 09 '19 at 05:17
  • 1
    That is really inconvenient! I have reqs to store settings in db after first load to prevent user messing with config file. I guess will have to store the whole json... Why it would be a problem to expose these configs read-only? In this case "differently"=drawback. – lentyai Nov 28 '19 at 03:36
1

I had to do something similar for a log reader built into the app, so I worked around this by installing the Serilog.Settings.AppSettings and read the config via the ConfigurationManager.

Since I have rolling enabled, I had to create a helper function to provide the actual filename of the log file.

public static string GetLogFileName()
{
        var filePathSetting = ConfigurationManager.AppSettings.Get("serilog:write-to:File.path");
        var rollingEnabled = ConfigurationManager.AppSettings.Get("serilog:write-to:File.rollingInterval");
        if (!string.IsNullOrEmpty(rollingEnabled))
        {
                if (rollingEnabled.Equals("Day"))
                {
                        Regex regex = new Regex(@"(\.[a-z]+$)", RegexOptions.IgnoreCase);
                        var matches = regex.Matches(filePathSetting);
                        if (matches.Count > 0)
                        {
                                filePathSetting = filePathSetting.Replace(matches[0].Value, DateTime.Now.ToString("yyyyMMdd") + matches[0].Value);
                        }
                        return filePathSetting;
                }
        }

        return filePathSetting.ToString();
}
bLiTzJoN
  • 11
  • 1