示例#1
0
        public void TestAutoReloadOnFileCopy()
        {
            string config1 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='${message}' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";
            string config2 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='[${message}]' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";

            string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempPath);

            string configFilePath = Path.Combine(tempPath, "reload.nlog");

            WriteConfigFile(configFilePath, config1);
            string otherFilePath = Path.Combine(tempPath, "other.nlog");

            try
            {
                LogManager.Configuration = new XmlLoggingConfiguration(configFilePath);

                var logger = LogManager.GetLogger("A");
                logger.Debug("aaa");
                AssertDebugLastMessage("debug", "aaa");

                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Delete(configFilePath);
                    reloadWaiter.WaitForReload();
                }

                logger.Debug("bbb");
                // Assert that config1 is still loaded.
                AssertDebugLastMessage("debug", "bbb");

                WriteConfigFile(otherFilePath, config2);
                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Copy(otherFilePath, configFilePath);
                    File.Delete(otherFilePath);

                    reloadWaiter.WaitForReload();
                    Assert.True(reloadWaiter.DidReload);
                }

                logger.Debug("ccc");
                // Assert that config2 is loaded.
                AssertDebugLastMessage("debug", "[ccc]");
            }
            finally
            {
                if (Directory.Exists(tempPath))
                {
                    Directory.Delete(tempPath, true);
                }
            }
        }
示例#2
0
        private static void ChangeAndReloadConfigFile(string configFilePath, string config, bool assertDidReload = true)
        {
            using (var reloadWaiter = new ConfigurationReloadWaiter())
            {
                WriteConfigFile(configFilePath, config);
                reloadWaiter.WaitForReload();

                if (assertDidReload)
                {
                    Assert.True(reloadWaiter.DidReload, "Config did not reload.");
                }
            }
        }
示例#3
0
        private static void ChangeAndReloadConfigFile(string configFilePath, string config, bool assertDidReload = true)
        {
            using (var reloadWaiter = new ConfigurationReloadWaiter())
            {
                WriteConfigFile(configFilePath, config);
                reloadWaiter.WaitForReload();

                if (assertDidReload)
                    Assert.True(reloadWaiter.DidReload, "Config did not reload.");
            }
        }
        public void TestAutoReloadOnFileCopy(bool useExplicitFileLoading)
        {
#if NETSTANDARD
            if (IsTravis())
            {
                Console.WriteLine("[SKIP] ReloadTests.TestAutoReloadOnFileCopy because we are running in Travis");
                return;
            }
#endif
            string config1 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='${message}' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";
            string config2 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='[${message}]' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";

            string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Directory.CreateDirectory(tempPath);

            string configFilePath = Path.Combine(tempPath, "reload.nlog");
            WriteConfigFile(configFilePath, config1);
            string otherFilePath = Path.Combine(tempPath, "other.nlog");

            try
            {
                SetLogManagerConfiguration(useExplicitFileLoading, configFilePath);

                var logger = LogManager.GetLogger("A");
                logger.Debug("aaa");
                AssertDebugLastMessage("debug", "aaa");

                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Delete(configFilePath);
                    reloadWaiter.WaitForReload();
                }

                logger.Debug("bbb");
                // Assert that config1 is still loaded.
                AssertDebugLastMessage("debug", "bbb");

                WriteConfigFile(otherFilePath, config2);
                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Copy(otherFilePath, configFilePath);
                    File.Delete(otherFilePath);

                    reloadWaiter.WaitForReload();
                    Assert.True(reloadWaiter.DidReload);
                }

                logger.Debug("ccc");
                // Assert that config2 is loaded.
                AssertDebugLastMessage("debug", "[ccc]");
            }
            finally
            {
                if (Directory.Exists(tempPath))
                {
                    Directory.Delete(tempPath, true);
                }
            }
        }
示例#5
0
        public void TestAutoReloadOnFileCopy()
        {
            string config1 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='${message}' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";
            string config2 = @"<nlog autoReload='true'>
                    <targets><target name='debug' type='Debug' layout='[${message}]' /></targets>
                    <rules><logger name='*' minlevel='Debug' writeTo='debug' /></rules>
                </nlog>";

            string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Directory.CreateDirectory(tempPath);

            string configFilePath = Path.Combine(tempPath, "reload.nlog");
            WriteConfigFile(configFilePath, config1);
            string otherFilePath = Path.Combine(tempPath, "other.nlog");

            try
            {
                LogManager.Configuration = new XmlLoggingConfiguration(configFilePath);

                var logger = LogManager.GetLogger("A");
                logger.Debug("aaa");
                AssertDebugLastMessage("debug", "aaa");

                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Delete(configFilePath);
                    reloadWaiter.WaitForReload();
                }

                logger.Debug("bbb");
                // Assert that config1 is still loaded.
                AssertDebugLastMessage("debug", "bbb");

                WriteConfigFile(otherFilePath, config2);
                using (var reloadWaiter = new ConfigurationReloadWaiter())
                {
                    File.Copy(otherFilePath, configFilePath);
                    File.Delete(otherFilePath);

                    reloadWaiter.WaitForReload();
                    Assert.True(reloadWaiter.DidReload);
                }

                logger.Debug("ccc");
                // Assert that config2 is loaded.
                AssertDebugLastMessage("debug", "[ccc]");
            }
            finally
            {
                if (Directory.Exists(tempPath))
                    Directory.Delete(tempPath, true);
            }
        }