Esempio n. 1
0
        public void KeepVariablesOnReloadWithStaticMode()
        {
            // Arrange
            string config     = @"<nlog autoReload='true'>
                                <variable name='maxArchiveDays' value='7' />
                                <targets>
                                    <target name='logfile' type='file' fileName='test.log' maxArchiveDays='${maxArchiveDays}' />
                                </targets>
                                <rules>
                                    <logger name='*' minLevel='Debug' writeTo='logfile' />
                                </rules>
                            </nlog>";
            var    logFactory = new LogFactory();

            logFactory.Configuration = XmlLoggingConfigurationMock.CreateFromXml(logFactory, config);

            var fileTarget  = logFactory.Configuration.AllTargets[0] as NLog.Targets.FileTarget;
            var beforeValue = fileTarget.MaxArchiveDays;

            // Act
            logFactory.Configuration.Variables["MaxArchiveDays"] = "42";
            logFactory.Configuration = logFactory.Configuration.Reload();
            fileTarget = logFactory.Configuration.AllTargets[0] as NLog.Targets.FileTarget;
            var afterValue = fileTarget.MaxArchiveDays;

            // Assert
            Assert.Equal(7, beforeValue);
            Assert.Equal(42, afterValue);
        }
Esempio n. 2
0
        public static XmlLoggingConfigurationMock CreateFromXml(LogFactory logFactory, string configXml)
        {
            var newConfig = new XmlLoggingConfigurationMock(logFactory, configXml);

            newConfig.LoadFromXmlContent(configXml, null);
            return(newConfig);
        }
Esempio n. 3
0
        public void TestKeepVariablesOnReloadAllowUpdate()
        {
            string config1 = @"<nlog autoReload='true' keepVariablesOnReload='true'>
                                <variable name='var1' value='' />
                                <variable name='var2' value='old_value2' />
                                <targets><target name='mem' type='memory' layout='${var:var2}' /></targets>
                                <rules><logger name='*' writeTo='mem' /></rules>
                            </nlog>";

            string config2 = @"<nlog autoReload='true' keepVariablesOnReload='true'>
                                <variable name='var1' value='' />
                                <variable name='var2' value='new_value2' />
                                <targets><target name='mem' type='memory' layout='${var:var2}' /></targets>
                                <rules><logger name='*' writeTo='mem' /></rules>
                            </nlog>";

            var logFactory = new LogFactory();
            var xmlConfig  = XmlLoggingConfigurationMock.CreateFromXml(logFactory, config1);

            logFactory.Configuration = xmlConfig;

            // Act
            logFactory.Configuration.Variables.Remove("var1");
            logFactory.Configuration.Variables.Add("var3", "new_value3");
            xmlConfig.ConfigXml      = config2;
            logFactory.Configuration = xmlConfig.Reload();

            // Assert
            var nullEvent = LogEventInfo.CreateNullEvent();

            Assert.Equal("", logFactory.Configuration.Variables["var1"].Render(nullEvent));
            Assert.Equal("new_value2", logFactory.Configuration.Variables["var2"].Render(nullEvent));
            Assert.Equal("new_value3", logFactory.Configuration.Variables["var3"].Render(nullEvent));
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        public override LoggingConfiguration Reload()
        {
            var newConfig = new XmlLoggingConfigurationMock(LogFactory, ConfigXml);

            newConfig.PrepareForReload(this);
            newConfig.LoadFromXmlContent(ConfigXml, null);
            return(newConfig);
        }
Esempio n. 6
0
        public void TestResetVariablesOnReload()
        {
            string config = @"<nlog autoReload='true' keepVariablesOnReload='false'>
                                <variable name='var1' value='' />
                                <variable name='var2' value='keep_value' />
                            </nlog>";


            LogManager.Configuration = XmlLoggingConfigurationMock.CreateFromXml(config);
            LogManager.Configuration.Variables["var1"] = "new_value";
            LogManager.Configuration.Variables["var3"] = "new_value3";
            LogManager.Configuration = LogManager.Configuration.Reload();
            Assert.Equal("", LogManager.Configuration.Variables["var1"].OriginalText);
            Assert.Equal("keep_value", LogManager.Configuration.Variables["var2"].OriginalText);
        }
Esempio n. 7
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);
        }