Ejemplo n.º 1
0
        internal void ReloadConfigOnTimer(object state)
        {
            LoggingConfiguration configurationToReload = (LoggingConfiguration)state;

            InternalLogger.Info("Reloading configuration...");
            lock (this.syncRoot)
            {
                if (this.reloadTimer != null)
                {
                    this.reloadTimer.Dispose();
                    this.reloadTimer = null;
                }

                if (this.IsDisposing)
                {
                    return; //timer was disposed already.
                }

                this.watcher.StopWatching();
                try
                {
                    if (this.Configuration != configurationToReload)
                    {
                        throw new NLogConfigurationException("Config changed in between. Not reloading.");
                    }

                    LoggingConfiguration newConfig = configurationToReload.Reload();

                    //problem: XmlLoggingConfiguration.Initialize eats exception with invalid XML. ALso XmlLoggingConfiguration.Reload never returns null.
                    //therefor we check the InitializeSucceeded property.

                    var xmlConfig = newConfig as XmlLoggingConfiguration;
                    if (xmlConfig != null)
                    {
                        if (!xmlConfig.InitializeSucceeded.HasValue || !xmlConfig.InitializeSucceeded.Value)
                        {
                            throw new NLogConfigurationException("Configuration.Reload() failed. Invalid XML?");
                        }
                    }

                    if (newConfig != null)
                    {
                        if (this.KeepVariablesOnReload)
                        {
                            newConfig.CopyVariables(this.Configuration.Variables);
                        }
                        this.Configuration = newConfig;
                        if (this.ConfigurationReloaded != null)
                        {
                            this.ConfigurationReloaded(this, new LoggingConfigurationReloadedEventArgs(true, null));
                        }
                    }
                    else
                    {
                        throw new NLogConfigurationException("Configuration.Reload() returned null. Not reloading.");
                    }
                }
                catch (Exception exception)
                {
                    //special case, don't rethrow NLogConfigurationException
                    if (exception is NLogConfigurationException)
                    {
                        InternalLogger.Warn(exception, "NLog configuration while reloading");
                    }
                    else if (exception.MustBeRethrown())
                    {
                        throw;
                    }

                    this.watcher.Watch(configurationToReload.FileNamesToWatch);

                    var configurationReloadedDelegate = this.ConfigurationReloaded;
                    if (configurationReloadedDelegate != null)
                    {
                        configurationReloadedDelegate(this, new LoggingConfigurationReloadedEventArgs(false, exception));
                    }
                }
            }
        }