Beispiel #1
0
        public void ConsoleOutTest()
        {
            var target = new ConsoleTarget()
            {
                Header = "-- header --",
                Layout = "${logger} ${message}",
                Footer = "-- footer --",
            };

            var consoleOutWriter = new StringWriter();
            TextWriter oldConsoleOutWriter = Console.Out;
            Console.SetOut(consoleOutWriter);

            try
            {
                var exceptions = new List<Exception>();
                target.Initialize(null);
                target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message1").WithContinuation(exceptions.Add));
                target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message2").WithContinuation(exceptions.Add));
                target.WriteAsyncLogEvents(
                    new LogEventInfo(LogLevel.Info, "Logger1", "message3").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger2", "message4").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger2", "message5").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger1", "message6").WithContinuation(exceptions.Add));
                Assert.AreEqual(6, exceptions.Count);
                target.Close();
            }
            finally 
            {
                Console.SetOut(oldConsoleOutWriter);
            }

            string expectedResult = @"-- header --
Logger1 message1
Logger1 message2
Logger1 message3
Logger2 message4
Logger2 message5
Logger1 message6
-- footer --
";
            Assert.AreEqual(expectedResult, consoleOutWriter.ToString());
        }
Beispiel #2
0
        public void ConsoleErrorTest()
        {
            var target = new ConsoleTarget()
            {
                Header = "-- header --",
                Layout = "${logger} ${message}",
                Footer = "-- footer --",
                Error = true,
            };

            var consoleErrorWriter = new StringWriter();
            TextWriter oldConsoleErrorWriter = Console.Error;
            Console.SetError(consoleErrorWriter);

            try
            {
                var exceptions = new List<Exception>();
                target.Initialize(null);
                target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message1").WithContinuation(exceptions.Add));
                target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message2").WithContinuation(exceptions.Add));
                target.WriteAsyncLogEvents(
                    new LogEventInfo(LogLevel.Info, "Logger1", "message3").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger2", "message4").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger2", "message5").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Info, "Logger1", "message6").WithContinuation(exceptions.Add));
                Assert.Equal(6, exceptions.Count);
                target.Close();
            }
            finally
            {
                Console.SetError(oldConsoleErrorWriter);
            }

            string expectedResult = string.Format("-- header --{0}Logger1 message1{0}Logger1 message2{0}Logger1 message3{0}Logger2 message4{0}Logger2 message5{0}Logger1 message6{0}-- footer --{0}", Environment.NewLine);
            Assert.Equal(expectedResult, consoleErrorWriter.ToString());
        }