internal void ReloadConfigOnTimer(object state)
{
if (_reloadTimer == null && _isDisposing)
{
return; //timer was disposed already.
}
LoggingConfiguration oldConfig = (LoggingConfiguration)state;
InternalLogger.Info("Reloading configuration...");
lock (_logFactory._syncRoot)
{
LoggingConfiguration newConfig;
try
{
if (_isDisposing)
{
return; //timer was disposed already.
}
var currentTimer = _reloadTimer;
if (currentTimer != null)
{
_reloadTimer = null;
currentTimer.WaitForDispose(TimeSpan.Zero);
}
if (_logFactory._config != oldConfig)
{
InternalLogger.Warn("NLog Config changed in between. Not reloading.");
return;
}
newConfig = oldConfig.Reload();
//problem: XmlLoggingConfiguration.Initialize eats exception with invalid XML. ALso XmlLoggingConfiguration.Reload never returns null.
//therefor we check the InitializeSucceeded property.
if (newConfig is XmlLoggingConfiguration xmlConfig && xmlConfig.InitializeSucceeded != true)
{
InternalLogger.Warn("NLog Config Reload() failed. Invalid XML?");
return;
}
}
catch (Exception exception)
{
if (exception.MustBeRethrownImmediately())
{
throw; // Throwing exceptions here will crash the entire application (.NET 2.0 behavior)
}
InternalLogger.Warn(exception, "NLog configuration failed to reload");
_logFactory?.NotifyConfigurationReloaded(new LoggingConfigurationReloadedEventArgs(false, exception));
return;
}
try
{
TryUnwatchConfigFile();
if (newConfig != null)
{
if (_logFactory.KeepVariablesOnReload && _logFactory._config != null)
{
newConfig.CopyVariables(_logFactory._config.Variables);
}
_logFactory.Configuration = newConfig; // Triggers LogFactory to call Activated(...) that adds file-watch again
_logFactory?.NotifyConfigurationReloaded(new LoggingConfigurationReloadedEventArgs(true));
}
}
catch (Exception exception)
{
if (exception.MustBeRethrownImmediately())
{
throw; // Throwing exceptions here will crash the entire application (.NET 2.0 behavior)
}
InternalLogger.Warn(exception, "NLog configuration reloaded, failed to be assigned");
_watcher.Watch(oldConfig.FileNamesToWatch);
_logFactory?.NotifyConfigurationReloaded(new LoggingConfigurationReloadedEventArgs(false, exception));
}
}
}