Exemplo n.º 1
0
        public void WhenConfigUpdated_VariablesAreRetained()
        {
            Directory.CreateDirectory("../logs.config");

            File.WriteAllText("../logs.config/nlog.web.config", "<nlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                              + "<variable name=\"name2\" value=\"server_secret\" />"
                              + "</nlog>");

            File.WriteAllText("nlog.config", "<nlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                              + "<variable name=\"name1\" value=\"value1\" />"
                              + "<variable name=\"name2\" value=\"value2\" />"
                              + "<variable name=\"name3\" value=\"value3\" />"
                              + "</nlog>");


            var old = DateTime.UtcNow - TimeSpan.FromHours(3);

            File.SetLastWriteTimeUtc("../logs.config/nlog.web.config", old);

            var file = LogConfig.PrepareConfigFile();

            var values = LogConfig.ReadVariables("../logs.config/nlog.web.config");

            values.Should().BeEquivalentTo(new Dictionary <string, string>()
            {
                { "name1", "value1" },
                { "name2", "server_secret" },
                { "name3", "value3" },
            });
        }
Exemplo n.º 2
0
        public void MultipleChanges_AreHandled()
        {
            File.WriteAllText("nlog.config", "<nlog>newer</nlog>");
            LogConfig.PrepareConfigFile();

            File.WriteAllText("nlog.config", "<nlog>newer</nlog>");
            LogConfig.PrepareConfigFile();
        }
Exemplo n.º 3
0
        public void WhenConfigDoesNotExists_ConfigFileIsCreated()
        {
            File.WriteAllText("nlog.config", "<nlog/>");

            var file = LogConfig.PrepareConfigFile();

            Path.IsPathFullyQualified(file).Should().BeTrue($"{file} should be fully qualified");
            File.Exists(file).Should().BeTrue("config should be created (in parent of app folder) if it does not already exist");
        }
Exemplo n.º 4
0
        public void WhenTargetConfigIsOlder_ItIsOverwritten()
        {
            Directory.CreateDirectory("../logs.config");
            File.WriteAllText("../logs.config/nlog.web.config", "<nlog>older</nlog>");
            File.WriteAllText("nlog.config", "<nlog>newer</nlog>");

            var old = DateTime.UtcNow - TimeSpan.FromHours(3);

            File.SetLastWriteTimeUtc("../logs.config/nlog.web.config", old);

            var file = LogConfig.PrepareConfigFile();

            File.ReadAllText(file).Should().EndWith("<nlog>newer</nlog>");
        }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            _startupTimer = new Stopwatch();
            _startupTimer.Start();

            IList <string> logSetupMessages = new List <string>();
            var            logConfigFile    = LogConfig.PrepareConfigFile(logSetupMessages);

            if (logConfigFile != null)
            {
                NLogBuilder.ConfigureNLog(logConfigFile);
            }

            var logger = LogManager.GetCurrentClassLogger();

            foreach (var logSetupMessage in logSetupMessages)
            {
                logger.Debug($"log setup: {logSetupMessage}");
            }

            logger.Info($"Logging configured using '{logConfigFile}'");

            try
            {
                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception e)
            {
                logger.Fatal(e, $"error from web host");
                throw;
            }
            finally
            {
                logger.Info($"Shutting down logging");
                LogManager.Shutdown();
            }
        }