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()); }
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()); }
public void ConsoleEncodingTest() { var consoleOutputEncoding = Console.OutputEncoding; var target = new ConsoleTarget() { Header = "-- header --", Layout = "${logger} ${message}", Footer = "-- footer --", Encoding = System.Text.Encoding.UTF8 }; Assert.Equal(System.Text.Encoding.UTF8, target.Encoding); var consoleOutWriter = new StringWriter(); TextWriter oldConsoleOutWriter = Console.Out; Console.SetOut(consoleOutWriter); try { var exceptions = new List<Exception>(); target.Initialize(null); // Not really testing whether Console.OutputEncoding works, but just that it is configured without breaking ConsoleTarget Assert.Equal(System.Text.Encoding.UTF8, Console.OutputEncoding); Assert.Equal(System.Text.Encoding.UTF8, target.Encoding); target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message1").WithContinuation(exceptions.Add)); target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "message2").WithContinuation(exceptions.Add)); Assert.Equal(2, exceptions.Count); target.Encoding = consoleOutputEncoding; Assert.Equal(consoleOutputEncoding, Console.OutputEncoding); target.Close(); } finally { Console.OutputEncoding = consoleOutputEncoding; Console.SetOut(oldConsoleOutWriter); } string expectedResult = string.Format("-- header --{0}Logger1 message1{0}Logger1 message2{0}-- footer --{0}", Environment.NewLine); Assert.Equal(expectedResult, consoleOutWriter.ToString()); }