Reuben Reuben - 14 days ago 8
C# Question

NLog autoreload does not work

I created a sample windows application to test NLog. I'm just writing all the message level inside a while loop (Trace, Debug, Warn & Error). I have set

autoReload='true'
and also specified
minlevel="Debug"
in the NLog config file. This is working fine and printing all the messages.

I wanted to test the autoreload option and hence changed the minlevel to "Warn" manually in the config file to log only Warning messages while the application is running, but still all the messages are getting logged instead of logging only Warn and Error messages.

But if I restart the application, it takes from the config and prints only Warn and Error level messages. As far as my understanding based on the link, NLog should reload the config as and when it is changed and should start printing the logs based on the current config changes. Not sure what is wrong. Do I have to programatically reload the configuration? Please help. Following is my code,

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log" >

<targets>

<target name="FileLogger" xsi:type="File" archiveAboveSize="1048576" fileName="Trace.log" archiveNumbering="DateAndSequence" />
</targets>

<rules>
<logger name="*" writeTo="FileLogger" minlevel="Debug" />
</rules>
</nlog>


LogMessage Code Snippet

private void LogMessages(CancellationToken token)
{
try
{
while (true)
{
logger.Debug("*********** Debug *************");
logger.Trace("----------- Trace --------------");
logger.Warn("~~~~~~~~~~~ Warn ~~~~~~~~~~~~~~");
logger.Error("========== Error =============");

if (token.IsCancellationRequested)
break;
}
}
catch (Exception ex)
{
throw ex;
}
}


I was suspecting whether this is happening because of thread/task and started running directly from main thread, but still the problem persists. I also tried to enable NLog internal logging to see for any errors, but couldn't find anything suspicious.

Answer

Have you tried setting the LogManager.GlobalThreshold property? Try doing that and don't forget to LogManager.ReconfigExistingLoggers() afterwards