1
votes

This seems like it would be an easy thing to do and would be a common desire, but I'm curious about a good way to do it. I would like to write the minlevel for a specific logger to its log file upon startup. I am routinely looking at logs and trying to remember how I left the log level for that application without having to dig up the NLog.config file. I would like a log entry that would look something like this:

2018-08-03 09:38:23.7975 | INFO | Starting with Log Level set to: Debug

I can see that I should be able to access the LoggingConfiguration as such:

NLog.LogManager.Configuration

But how do I access the rules and minlevel from that? Any other suggestions? If you can suggest some good NLog resources in addition to the NLog Tutorial, I would appreciate it.

2
Check if logger.IsTraceEnabled is true, if not, check if IsDebugEnabled is true, if not, check if IsInfoEnabled...?user47589

2 Answers

3
votes

Thank you for the comments and answers. Severius5, your answer (deleted now?) gave me my answer. This code does what I need:

        var rules = NLog.LogManager.Configuration.LoggingRules;
        var rule = rules.FirstOrDefault(x => x.LoggerNamePattern == "hostLogger");
        NLog.LogManager.GetLogger("hostLogger").Info("Minimum Log Level: {0}", rule.Levels.Min());

It gave me this output in the log:

2018-08-03 14:26:11.7439 | INFO | Minimum Log Level: Debug

2
votes

Maybe something like this:

var logger = NLog.LogManager.GetLogger("hostLogger");
var minLogLevel = "Disabled";
if (logger.IsTraceEnabled)
    minLogLevel = "Trace";
else if (logger.IsDebugEnabled)
    minLogLevel = "Debug";
else if (logger.IsInfoEnabled)
    minLogLevel = "Info";
else if (logger.IsWarnEnabled)
    minLogLevel = "Warn";
else if (logger.IsErrorEnabled)
    minLogLevel = "Error";
else if (logger.IsFatalEnabled)
    minLogLevel = "Fatal";
logger.Info("Minimum Log Level: {0}", minLogLevel)