change log levels at runtime

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

change log levels at runtime

nehawadhwa
hi,
how can i make changes to configuration file at runtime?
what do i do to switch from one log level to another at runtime?

Please help me here...

thanks...
Reply | Threaded
Open this post in threaded view
|

Re: change log levels at runtime

Jason LaFlair
Administrator
Being the log level is sent by code and not config, you'd just need to switch up your logging method.

(cut/paste from Tutorial)
logger.Trace("Sample trace message");
logger.Debug("Sample debug message");
logger.Info("Sample informational message");
logger.Warn("Sample warning message");
logger.Error("Sample error message");
logger.Fatal("Sample fatal error message");

Or just specify your log level before hand and call the .Log method
var level = LogLevel.Debug;
logger.Log(level, "Sample message");
Reply | Threaded
Open this post in threaded view
|

Re: change log levels at runtime

nehawadhwa
Thanks Jason...

I really appreciate your response...

So, does it mean that we cannot switch to different levels at runtime?
Suppose I want to start debugging at runtime, then I cannot change the loglevel in code to debug if it was something else before? if yes, how?

also, on very adhoc basis, I get this error when debugging that source code was not available when I try to create instance of logger class...can u please tell me the reason for it and how i can fix it?
when we say Debug is the target..what does it exactly mean? What is debug target?

I am very new to NLog and doing it first time for my project...will appreciate ur help here..

thanks


Reply | Threaded
Open this post in threaded view
|

Re: change log levels at runtime

Jason LaFlair
Administrator
With NLog - the log level is set at the message level.  So there isn't a 'global' log level to set.

Here is an email I hope helps.

public static void SimpleExampleMethod()
        {
            try
            {
                logger.Debug("starting method");
                // or
                logger.Log(LogLevel.Debug, "starting method");


                // SOME WORKING CODE HERE
                logger.Info("Doing work");
                // or
                logger.Log(LogLevel.Info, "Doing work");


                logger.Debug("Method completed successfully");
                // or
                logger.Log(LogLevel.Debug, "Method completed successfully");
            }
            catch (Exception ex)
            {
                logger.Fatal("Error");

                // or use
                logger.FatalException("Error", ex);
            }
        }

Have you walked through the Tutorial yet?

From some of your further questions it seems you may be confusing the layers of Logger/Rule/Target and I think the tutorial might help.

Let us know
Reply | Threaded
Open this post in threaded view
|

Re: change log levels at runtime

Dave
I think what the op wants to do is change the minLevel for a logger. Meaning that if my configuration has these rules at compile time, how do I override that at runtime under certain conditions?

        <rules>
                <logger name="*" minlevel="Warn" writeTo="file" />
                <logger name="*" writeTo="database" minlevel="Warn" />
               
                <logger name="*" minlevel="Warn" writeTo="mail" />
        </rules>
Reply | Threaded
Open this post in threaded view
|

Re: change log levels at runtime

Dave
I wrote this method which appears to do this:

        using System.Linq;
        using System.Collections.Generic;
        using System.Text.RegularExpressions;
        using NLog;

        private static void setLogLevel(LogLevel logLevel, string regex)
        {
            IList<NLog.Config.LoggingRule> rules = LogManager.Configuration.LoggingRules;
            Regex validator = new Regex(regex);
            foreach (var rule in rules.Where(x => validator.IsMatch(x.Targets[0].Name)))
            {
                if (!rule.IsLoggingEnabledForLevel(logLevel))
                {
                    rule.EnableLoggingForLevel(logLevel);
                }
            }
        }

calling it like this would add Debug logging only for my mail and database targets:                

        setLogLevel(LogLevel.Debug, "(mail|database)");