Beispiel #1
0
        public void TestKeepVariablesOnReload()
        {
            string config = @"<nlog autoReload='true' keepVariablesOnReload='true'>
                                <variable name='var1' value='' />
                                <variable name='var2' value='keep_value' />
                            </nlog>";

            var configLoader  = new LoggingConfigurationWatchableFileLoader(LogFactory.DefaultAppEnvironment);
            var logFactory    = new LogFactory(configLoader);
            var configuration = XmlLoggingConfigurationMock.CreateFromXml(logFactory, config);

            logFactory.Configuration = configuration;
            logFactory.Configuration.Variables["var1"] = "new_value";
            logFactory.Configuration.Variables["var3"] = "new_value3";
            configLoader.ReloadConfigOnTimer(configuration);
            var nullEvent = LogEventInfo.CreateNullEvent();

            Assert.Equal("new_value", logFactory.Configuration.Variables["var1"].Render(nullEvent));
            Assert.Equal("keep_value", logFactory.Configuration.Variables["var2"].Render(nullEvent));
            Assert.Equal("new_value3", logFactory.Configuration.Variables["var3"].Render(nullEvent));

            logFactory.Configuration = configuration.Reload();
            Assert.Equal("new_value", logFactory.Configuration.Variables["var1"].Render(nullEvent));
            Assert.Equal("keep_value", logFactory.Configuration.Variables["var2"].Render(nullEvent));
            Assert.Equal("new_value3", logFactory.Configuration.Variables["var3"].Render(nullEvent));
        }
Beispiel #2
0
        public void ReloadConfigOnTimer_DoesNotThrowConfigException_IfConfigReloadReturnsNull()
        {
            var loggingConfiguration = new ReloadNullConfiguration();

            LogManager.Configuration = loggingConfiguration;
            var configLoader = new LoggingConfigurationWatchableFileLoader(LogFactory.DefaultAppEnvironment);
            var logFactory   = new LogFactory(configLoader);

            logFactory.Configuration = loggingConfiguration;

            var exRecorded = Record.Exception(() => configLoader.ReloadConfigOnTimer(loggingConfiguration));

            Assert.Null(exRecorded);
        }
Beispiel #3
0
        public void TestResetVariablesOnReload()
        {
            string config = @"<nlog autoReload='true' keepVariablesOnReload='false'>
                                <variable name='var1' value='' />
                                <variable name='var2' value='keep_value' />
                            </nlog>";

            var configLoader  = new LoggingConfigurationWatchableFileLoader();
            var logFactory    = new LogFactory(configLoader);
            var configuration = XmlLoggingConfigurationMock.CreateFromXml(logFactory, config);

            logFactory.Configuration = configuration;
            logFactory.Configuration.Variables["var1"] = "new_value";
            logFactory.Configuration.Variables["var3"] = "new_value3";
            configLoader.ReloadConfigOnTimer(configuration);
            Assert.Equal("", logFactory.Configuration.Variables["var1"].OriginalText);
            Assert.Equal("keep_value", logFactory.Configuration.Variables["var2"].OriginalText);
        }
        public void ReloadConfigOnTimer_When_No_Exception_Raises_ConfigurationReloadedEvent()
        {
            var called = false;
            LoggingConfigurationReloadedEventArgs arguments = null;
            object calledBy = null;

            var configLoader         = new LoggingConfigurationWatchableFileLoader(LogFactory.DefaultAppEnvironment);
            var logFactory           = new LogFactory(configLoader);
            var loggingConfiguration = XmlLoggingConfigurationMock.CreateFromXml(logFactory, "<nlog></nlog>");

            logFactory.Configuration          = loggingConfiguration;
            logFactory.ConfigurationReloaded += (sender, args) => { called = true; calledBy = sender; arguments = args; };

            configLoader.ReloadConfigOnTimer(loggingConfiguration);

            Assert.True(called);
            Assert.Same(calledBy, logFactory);
            Assert.True(arguments.Succeeded);
        }
Beispiel #5
0
        public void ReloadConfigOnTimer_When_No_Exception_Raises_ConfigurationReloadedEvent()
        {
            var called = false;
            LoggingConfigurationReloadedEventArgs arguments = null;
            object calledBy             = null;
            var    loggingConfiguration = new LoggingConfiguration();

            LogManager.Configuration = loggingConfiguration;
            var configLoader = new LoggingConfigurationWatchableFileLoader();
            var logFactory   = new LogFactory(configLoader);

            logFactory.Configuration          = loggingConfiguration;
            logFactory.ConfigurationReloaded += (sender, args) => { called = true; calledBy = sender; arguments = args; };

            configLoader.ReloadConfigOnTimer(loggingConfiguration);

            Assert.True(called);
            Assert.Same(calledBy, logFactory);
            Assert.True(arguments.Succeeded);
        }
Beispiel #6
0
        public void ReloadConfigOnTimer_DoesNotThrowConfigException_IfConfigChangedInBetween()
        {
            EventHandler <LoggingConfigurationChangedEventArgs> testChanged = null;

            try
            {
                LogManager.Configuration = null;

                var loggingConfiguration = new LoggingConfiguration();
                LogManager.Configuration = loggingConfiguration;

                var configLoader = new LoggingConfigurationWatchableFileLoader(LogFactory.DefaultAppEnvironment);
                var logFactory   = new LogFactory(configLoader);
                logFactory.Configuration = loggingConfiguration;

                var differentConfiguration = new LoggingConfiguration();

                // Verify that the random configuration change is ignored (Only the final reset is reacted upon)
                bool called = false;
                LoggingConfiguration oldConfiguration = null, newConfiguration = null;
                testChanged = (s, e) => { called = true; oldConfiguration = e.DeactivatedConfiguration; newConfiguration = e.ActivatedConfiguration; };
                LogManager.LogFactory.ConfigurationChanged += testChanged;

                var exRecorded = Record.Exception(() => configLoader.ReloadConfigOnTimer(differentConfiguration));
                Assert.Null(exRecorded);

                // Final reset clears the configuration, so it is changed to null
                LogManager.Configuration = null;
                Assert.True(called);
                Assert.Equal(loggingConfiguration, oldConfiguration);
                Assert.Null(newConfiguration);
            }
            finally
            {
                if (testChanged != null)
                {
                    LogManager.LogFactory.ConfigurationChanged -= testChanged;
                }
            }
        }