Ejemplo n.º 1
0
        public void LoggerLogsWithEnrichers()
        {
            var provider = new SerilogDynamicProvider(GetConfigurationFromFile());

            var fac = new LoggerFactory();

            fac.AddProvider(provider);
            var logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));

            // act I - log at all levels, expect Info and above to work
            using var unConsole = new ConsoleOutputBorrower();
            using (LogContext.PushProperty("A", 1))
            {
                logger.LogInformation("Carries property A = 1");

                using (LogContext.PushProperty("A", 2))
                    using (LogContext.PushProperty("B", 1))
                    {
                        logger.LogInformation("Carries A = 2 and B = 1");
                    }

                logger.LogInformation("Carries property A = 1, again");
            }

            var logged = unConsole.ToString();

            // assert I
            Assert.Contains(@"A.B.C.D.TestClass: {A=1, Application=""Sample""}", logged);
            Assert.Contains(@"Carries property A = 1", logged);
            Assert.Contains(@"A.B.C.D.TestClass: {B=1, A=2, Application=""Sample""}", logged);
            Assert.Contains(@"Carries A = 2 and B = 1", logged);
            Assert.Contains(@"A.B.C.D.TestClass: {A=1, Application=""Sample""}", logged);
            Assert.Contains(@"Carries property A = 1, again", logged);
            Assert.Matches(new Regex(@"ThreadId:<\d+>"), logged);
        }
Ejemplo n.º 2
0
        public void LoggerLogsWithDestructuring()
        {
            var provider = new SerilogDynamicProvider(GetConfigurationFromFile());
            var fac      = new LoggerFactory();

            fac.AddProvider(provider);
            var logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));

            // act I - log at all levels, expect Info and above to work
            using var unConsole = new ConsoleOutputBorrower();
            logger.LogInformation("Info {@TestInfo}", new { Info1 = "information1", Info2 = "information2" });

            var logged = unConsole.ToString();

            Assert.Contains(@"Info { Info1 = information1, Info2 = information2 }", logged);
        }
        public void LoggerLogsWithDestructuring()
        {
            // arrange
            var provider = new SerilogDynamicProvider(GetConfigurationFromFile());
            var fac      = new LoggerFactory();

            fac.AddProvider(provider);
            var logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));

            // act I - log at all levels, expect Info and above to work
            using var unConsole = new ConsoleOutputBorrower();
            logger.LogInformation("Info {@TestInfo}", new { Info1 = "information1", Info2 = "information2" });

            // pause the thread to allow the logging to happen
            Thread.Sleep(100);

            var logged = unConsole.ToString();

            // assert I
            Assert.Contains(@"  Info {""Info1"": ""inf…"", ""Info2"": ""inf…""}", logged);
        }
        public void LoggerLogs_At_Configured_Setting()
        {
            // arrange
            var provider = new SerilogDynamicProvider(GetConfiguration());
            var fac      = new LoggerFactory();

            fac.AddProvider(provider);
            var logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));

            // act I - log at all levels, expect Info and above to work
            using (var unConsole = new ConsoleOutputBorrower())
            {
                WriteLogEntries(logger);

                // pause the thread to allow the logging to happen
                Thread.Sleep(100);

                var logged = unConsole.ToString();

                // assert I
                Assert.Contains("Critical message", logged);
                Assert.Contains("Error message", logged);
                Assert.Contains("Warning message", logged);
                Assert.Contains("Informational message", logged);
                Assert.DoesNotContain("Debug message", logged);
                Assert.DoesNotContain("Trace message", logged);
            }

            // act II - adjust rules, expect Error and above to work
            provider.SetLogLevel("A.B.C.D", LogLevel.Error);
            using (var unConsole = new ConsoleOutputBorrower())
            {
                WriteLogEntries(logger);

                // pause the thread to allow the logging to happen
                Thread.Sleep(100);

                var logged2 = unConsole.ToString();

                // assert II
                Assert.Contains("Critical message", logged2);
                Assert.Contains("Error message", logged2);
                Assert.DoesNotContain("Warning message", logged2);
                Assert.DoesNotContain("Informational message", logged2);
                Assert.DoesNotContain("Debug message", logged2);
                Assert.DoesNotContain("Trace message", logged2);
            }

            // act III - adjust rules, expect Trace and above to work
            provider.SetLogLevel("A", LogLevel.Trace);
            using (var unConsole = new ConsoleOutputBorrower())
            {
                WriteLogEntries(logger);

                // pause the thread to allow the logging to happen
                Thread.Sleep(100);

                var logged3 = unConsole.ToString();

                // assert III
                Assert.Contains("Critical message", logged3);
                Assert.Contains("Error message", logged3);
                Assert.Contains("Warning message", logged3);
                Assert.Contains("Informational message", logged3);
                Assert.Contains("Debug message", logged3);
                Assert.Contains("Trace message", logged3);
            }

            // act IV - adjust rules, expect nothing to work
            provider.SetLogLevel("A", LogLevel.None);
            using (var unConsole = new ConsoleOutputBorrower())
            {
                WriteLogEntries(logger);

                // pause the thread to allow the logging to happen
                Thread.Sleep(100);

                var logged4 = unConsole.ToString();

                // assert IV
                Assert.Contains("Critical message", logged4); // Serilog lowest level is Fatal == Critical
                Assert.DoesNotContain("Error message", logged4);
                Assert.DoesNotContain("Warning message", logged4);
                Assert.DoesNotContain("Informational message", logged4);
                Assert.DoesNotContain("Debug message", logged4);
                Assert.DoesNotContain("Trace message", logged4);
            }

            // act V - reset the rules, expect Info and above to work
            provider.SetLogLevel("A", LogLevel.Information); // Only works with serilog for configured values
            using (var unConsole = new ConsoleOutputBorrower())
            {
                WriteLogEntries(logger);

                // pause the thread to allow the logging to happen
                Thread.Sleep(100);

                var logged5 = unConsole.ToString();

                // assert V
                Assert.NotNull(provider.GetLoggerConfigurations().First(c => c.Name == "A"));
                Assert.Contains("Critical message", logged5);
                Assert.Contains("Error message", logged5);
                Assert.Contains("Warning message", logged5);
                Assert.Contains("Informational message", logged5);
                Assert.DoesNotContain("Debug message", logged5);
                Assert.DoesNotContain("Trace message", logged5);
            }
        }