public void AsyncProxy_LogEntriesAreWrittenToSinks() { LogConfiguration configuration = new LogConfiguration(); AsyncProxy asyncProxy = new AsyncProxy(); ManualResetEvent stoppedEvent = new ManualResetEvent(false); TestSink sink = new TestSink(stoppedEvent); asyncProxy.Sinks.Add(sink); configuration.Sinks.Add(asyncProxy); using (LogKernel kernel = new LogKernel(configuration)) { ILogger logger = kernel.GetLogger(); logger.Write(LogLevel.Information, "A message"); // Wait for the entry to be written. if (!stoppedEvent.WaitOne(TimeSpan.FromSeconds(3))) { throw new TimeoutException("Timed out while waiting for entries to be written to sink."); } // Assert that the entry was written to the sink. Assert.IsNotNull(sink.Entry); Assert.AreEqual("A message", sink.Entry.Message); Assert.AreEqual(1, sink.WrittenEntries); } }
private string Render(string format, LogLevel level) { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create(format); ILogEntry entry = new LogEntry(DateTimeOffset.Now, level, "The log message.", logger, null); return pattern.Render(entry); }
public void FormatPattern_RenderEscapedRenderer() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("$$(time(format='HH:mm:ss'))"); ILogEntry entry = new LogEntry(DateTime.Now, LogLevel.Information, "The log message.", logger, null); Assert.AreEqual("$(time(format='HH:mm:ss'))", pattern.Render(entry)); }
public void FormatPatternFactory_BuildPatternWithTransformer() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("$(uppercase(message()))"); ILogEntry entry = new LogEntry(DateTimeOffset.Now, LogLevel.Information, "The log message.", logger, null); string rendered = pattern.Render(entry); Assert.IsNotNullOrEmpty(rendered); Assert.AreEqual("THE LOG MESSAGE.", rendered); }
public void FormatPatternFactory_BuildPatternWithNestedTransformers() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("$(rot13(uppercase(message())))"); ILogEntry entry = new LogEntry(DateTimeOffset.Now, LogLevel.Information, "Hello World!", logger, null); string rendered = pattern.Render(entry); Assert.IsNotNullOrEmpty(rendered); Assert.AreEqual("URYYB JBEYQ!", rendered); }
public void FormatPatternFactory_BuildComplexPattern() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("[$(time(format='HH:mm:ss'))] $(uppercase(rot13(message())))"); DateTimeOffset currentTime = DateTimeOffset.Now; ILogEntry entry = new LogEntry(currentTime, LogLevel.Information, "Hello World!", logger, null); string rendered = pattern.Render(entry); string expected = string.Format("[{0:HH:mm:ss}] URYYB JBEYQ!", currentTime); Assert.IsNotNullOrEmpty(rendered); Assert.AreEqual(expected, rendered); }
public void BufferProxy_RemainingMessagesAreFlushedWhenBufferProxyIsDisposed() { LogConfiguration configuration = new LogConfiguration(); BufferProxy proxy = new BufferProxy { BufferSize = 3 }; TestSink sink = new TestSink(); proxy.Sinks.Add(sink); configuration.Sinks.Add(proxy); LogKernel kernel = new LogKernel(configuration); ILogger logger = kernel.GetLogger(); Assert.AreEqual(0, sink.MessagesWritten); logger.Write(LogLevel.Information, "Hello World!"); logger.Write(LogLevel.Information, "Hello World Again!"); Assert.AreEqual(0, sink.MessagesWritten); configuration.Dispose(); Assert.AreEqual(2, sink.MessagesWritten); }
public void LogSinkProxy_UnhandledExceptionsInLogSinkProxyAreHandledAndWrittenToInternalLogWhenWritingMoreThanOneMessage_LogSinkGotNoName() { // This test is using the buffer proxy to buffer up messages and // indirectly use the LogSink.Write(ILogEntry[]) overload on the sink. LogConfiguration configuration = new LogConfiguration(); configuration.InternalLogger.Enabled = true; BufferProxy funnelProxy = new BufferProxy() { BufferSize = 0 }; funnelProxy.Sinks.Add(new ThrowSink(() => new InvalidOperationException("Hello World!"))); configuration.Sinks.Add(funnelProxy); LogKernel kernel = new LogKernel(configuration); ILogger logger = kernel.GetLogger(); string expected = "[BLACKBOX] An unnamed sink of type 'BlackBox.UnitTests.Tests.LogSinkTests+ThrowSink' threw an exception. Hello World!"; using (StringTraceListenerScope scope = new StringTraceListenerScope()) { logger.Write(LogLevel.Information, "TestMessage"); Assert.AreEqual(expected, scope.Listener.Messages[0]); } }
public void FormatPattern_RenderLiteralPattern() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("Hello World!"); ILogEntry entry = new LogEntry(DateTimeOffset.Now, LogLevel.Information, "The log message.", logger, null); string rendered = pattern.Render(entry); Assert.IsNotNullOrEmpty(rendered); Assert.AreEqual("Hello World!", rendered); }
public void FormatPattern_RenderTimeRenderer() { LogKernel kernel = new LogKernel(new LogConfiguration()); ILogger logger = kernel.GetLogger(); FormatPatternFactory factory = new FormatPatternFactory(); FormatPattern pattern = factory.Create("$(time(format='HH:mm:ss'))"); DateTimeOffset currentTime = DateTimeOffset.Now; ILogEntry entry = new LogEntry(currentTime, LogLevel.Information, "The log message.", logger, null); string rendered = pattern.Render(entry); Assert.IsNotNullOrEmpty(rendered); Assert.AreEqual(currentTime.ToString("HH:mm:ss"), rendered); }
public void LogSink_LogSinkCanWriteToInternalLogger() { LogConfiguration configuration = new LogConfiguration(); configuration.InternalLogger.Enabled = true; configuration.Sinks.Add(new TestSink()); LogKernel kernel = new LogKernel(configuration); ILogger logger = kernel.GetLogger(); using (StringTraceListenerScope scope = new StringTraceListenerScope()) { Assert.AreEqual(0, scope.Listener.Messages.Count); logger.Write(LogLevel.Information, "TestMessage"); Assert.AreEqual(1, scope.Listener.Messages.Count); Assert.AreEqual("TestMessage", scope.Listener.Messages[0]); } }
public void LogSink_UnhandledExceptionsInLogSinkAreHandledAndWrittenToInternalLog_LogSinkGotNoName() { LogConfiguration configuration = new LogConfiguration(); configuration.InternalLogger.Enabled = true; configuration.Sinks.Add(new ThrowSink(() => new InvalidOperationException("Hello World!"))); LogKernel kernel = new LogKernel(configuration); ILogger logger = kernel.GetLogger(); string expected = "[BLACKBOX] An unnamed sink of type 'BlackBox.UnitTests.Tests.LogSinkTests+ThrowSink' threw an exception. Hello World!"; using (StringTraceListenerScope scope = new StringTraceListenerScope()) { logger.Write(LogLevel.Information, "TestMessage"); Assert.AreEqual(expected, scope.Listener.Messages[0]); } }