public void LoggerProviderDoesNotWriteLogMessagesBelowMinimumLevel()
        {
            var testTestOutputHelper = new TestTestOutputHelper();
            var loggerFactory        = TestLoggerBuilder.Create(builder => builder
                                                                .AddXunit(testTestOutputHelper, LogLevel.Warning));

            var logger = loggerFactory.CreateLogger("TestCategory");

            logger.LogInformation("This is some great information");
            logger.LogError("This is a bad error");

            Assert.Equal("| TestCategory Error: This is a bad error" + Environment.NewLine, testTestOutputHelper.Output);
        }
        public void LoggerProviderDoesNotThrowIfOutputHelperThrows()
        {
            var testTestOutputHelper = new TestTestOutputHelper();
            var loggerFactory        = TestLoggerBuilder.Create(builder => builder
                                                                .AddXunit(testTestOutputHelper));

            testTestOutputHelper.Throw = true;

            var logger = loggerFactory.CreateLogger("TestCategory");

            logger.LogInformation("This is a" + Environment.NewLine + "multi-line" + Environment.NewLine + "message");

            Assert.Equal(0, testTestOutputHelper.Output.Length);
        }
Exemple #3
0
        public void TestLogWritesToITestOutputHelper()
        {
            var output      = new TestTestOutputHelper();
            var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null);

            using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory))
            {
                var logger = loggerFactory.CreateLogger("TestLogger");
                logger.LogInformation("Information!");
            }

            Assert.Equal(@"| TestLifetime Information: Starting test TestLogWritesToITestOutputHelper
| TestLogger Information: Information!
| TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION
", MakeConsistent(output.Output));
        }
        public void LoggerProviderPrependsPrefixToEachLine()
        {
            var testTestOutputHelper = new TestTestOutputHelper();
            var loggerFactory        = TestLoggerBuilder.Create(builder => builder
                                                                .AddXunit(testTestOutputHelper));

            var logger = loggerFactory.CreateLogger("TestCategory");

            logger.LogInformation("This is a" + Environment.NewLine + "multi-line" + Environment.NewLine + "message");

            var expectedOutput =
                "| TestCategory Information: This is a" + Environment.NewLine +
                "|                           multi-line" + Environment.NewLine +
                "|                           message" + Environment.NewLine;

            Assert.Equal(expectedOutput, testTestOutputHelper.Output);
        }
        public void LoggerProviderPrependsPrefixToEachLine()
        {
            var testTestOutputHelper = new TestTestOutputHelper();
            var loggerFactory        = TestLoggerBuilder.Create(builder => builder
                                                                .AddXunit(testTestOutputHelper));

            var logger = loggerFactory.CreateLogger("TestCategory");

            logger.LogInformation("This is a" + Environment.NewLine + "multi-line" + Environment.NewLine + "message");

            // The lines after the first one are indented more because the indentation was calculated based on the timestamp's actual length.
            var expectedOutput =
                "| [TIMESTAMP] TestCategory Information: This is a" + Environment.NewLine +
                "|                                                 multi-line" + Environment.NewLine +
                "|                                                 message" + Environment.NewLine;

            Assert.Equal(expectedOutput, MakeConsistent(testTestOutputHelper.Output));
        }
        public void LoggerProviderWritesToTestOutputHelper()
        {
            var testTestOutputHelper = new TestTestOutputHelper();

            var loggerFactory = TestLoggerBuilder.Create(builder => builder
                                                         .SetMinimumLevel(LogLevel.Trace)
                                                         .AddXunit(testTestOutputHelper));

            var logger = loggerFactory.CreateLogger("TestCategory");

            logger.LogInformation("This is some great information");
            logger.LogTrace("This is some unimportant information");

            var expectedOutput =
                "| TestCategory Information: This is some great information" + Environment.NewLine +
                "| TestCategory Trace: This is some unimportant information" + Environment.NewLine;

            Assert.Equal(expectedOutput, testTestOutputHelper.Output);
        }
        public void TestLogWritesToITestOutputHelper()
        {
            var output      = new TestTestOutputHelper();
            var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null);

            using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory))
            {
                var logger = loggerFactory.CreateLogger("TestLogger");
                logger.LogInformation("Information!");

                // Trace is disabled by default
                logger.LogTrace("Trace!");
            }

            Assert.Equal(@"[TIMESTAMP] TestLifetime Information: Starting test TestLogWritesToITestOutputHelper
[TIMESTAMP] TestLogger Information: Information!
[TIMESTAMP] TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION
", MakeConsistent(output.Output), ignoreLineEndingDifferences: true);
        }