public void LogLevel_LogValuesAndError_LogsCorrectValues(LogLevel logLevel) { // Arrange var sink = new TestSink(); var logger = SetUp(sink); var testLogValues = new TestLogValues() { Value = 1 }; // Act logger.Log(logLevel, 0, _exception, testLogValues.ToString()); // Assert Assert.True(sink.Writes.TryTake(out var write)); Assert.Equal(logLevel, write.LogLevel); Assert.Equal(0, write.EventId); Assert.Equal(_exception, write.Exception); Assert.Equal( "Test 1", write.Formatter(write.State, write.Exception)); }
public void BeginScope_CreatesScope_WithFormatStringValues() { // Arrange var testSink = new TestSink( writeEnabled: (writeContext) => true, beginEnabled: (beginScopeContext) => true); var logger = new TestLogger("TestLogger", testSink, enabled: true); var actionName = "App.Controllers.Home.Index"; var expectedStringMessage = "Executing action " + actionName; // Act var scope = logger.BeginScope("Executing action {ActionName}", actionName); // Assert var sinkScope = Assert.Single(testSink.Scopes); Assert.IsType<FormattedLogValues>(sinkScope.Scope); var scopeState = (FormattedLogValues)sinkScope.Scope; Assert.Equal(expectedStringMessage, scopeState.ToString()); Assert.True(scopeState.Count > 0); Assert.Contains(scopeState, (kvp) => { return (string.Equals(kvp.Key, "ActionName") && string.Equals(kvp.Value?.ToString(), actionName)); }); }
public void LogValuesEventIdAndError_LogsCorrectValues() { // Arrange var sink = new TestSink(); var logger = SetUp(sink); var testLogValues = new TestLogValues() { Value = 1 }; // Act logger.LogVerbose(1, testLogValues, _exception); logger.LogInformation(2, testLogValues, _exception); logger.LogWarning(3, testLogValues, _exception); logger.LogError(4, testLogValues, _exception); logger.LogCritical(5, testLogValues, _exception); logger.LogDebug(6, testLogValues, _exception); // Assert Assert.Equal(6, sink.Writes.Count); var verbose = sink.Writes[0]; Assert.Equal(LogLevel.Verbose, verbose.LogLevel); Assert.Same(testLogValues, verbose.State); Assert.Equal(1, verbose.EventId); Assert.Equal(_exception, verbose.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, verbose.Formatter(verbose.State, verbose.Exception)); var information = sink.Writes[1]; Assert.Equal(LogLevel.Information, information.LogLevel); Assert.Same(testLogValues, information.State); Assert.Equal(2, information.EventId); Assert.Equal(_exception, information.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, information.Formatter(information.State, information.Exception)); var warning = sink.Writes[2]; Assert.Equal(LogLevel.Warning, warning.LogLevel); Assert.Same(testLogValues, warning.State); Assert.Equal(3, warning.EventId); Assert.Equal(_exception, warning.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, warning.Formatter(warning.State, warning.Exception)); var error = sink.Writes[3]; Assert.Equal(LogLevel.Error, error.LogLevel); Assert.Same(testLogValues, error.State); Assert.Equal(4, error.EventId); Assert.Equal(_exception, error.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, error.Formatter(error.State, error.Exception)); var critical = sink.Writes[4]; Assert.Equal(LogLevel.Critical, critical.LogLevel); Assert.Same(testLogValues, critical.State); Assert.Equal(5, critical.EventId); Assert.Equal(_exception, critical.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, critical.Formatter(critical.State, critical.Exception)); var debug = sink.Writes[4]; Assert.Equal(LogLevel.Debug, debug.LogLevel); Assert.Same(testLogValues, debug.State); Assert.Equal(6, debug.EventId); Assert.Equal(_exception, debug.Exception); Assert.Equal( "Test 1" + Environment.NewLine + _exception, debug.Formatter(debug.State, debug.Exception)); }
public void LogValues_LogsCorrectValues() { // Arrange var sink = new TestSink(); var logger = SetUp(sink); var testLogValues = new TestLogValues() { Value = 1 }; // Act logger.LogVerbose(testLogValues); logger.LogInformation(testLogValues); logger.LogWarning(testLogValues); logger.LogError(testLogValues); logger.LogCritical(testLogValues); logger.LogDebug(testLogValues); // Assert Assert.Equal(6, sink.Writes.Count); var verbose = sink.Writes[0]; Assert.Equal(LogLevel.Verbose, verbose.LogLevel); Assert.Same(testLogValues, verbose.State); Assert.Equal(0, verbose.EventId); Assert.Equal(null, verbose.Exception); Assert.Equal("Test 1", verbose.Formatter(verbose.State, verbose.Exception)); var information = sink.Writes[1]; Assert.Equal(LogLevel.Information, information.LogLevel); Assert.Same(testLogValues, information.State); Assert.Equal(0, information.EventId); Assert.Equal(null, information.Exception); Assert.Equal("Test 1", information.Formatter(information.State, information.Exception)); var warning = sink.Writes[2]; Assert.Equal(LogLevel.Warning, warning.LogLevel); Assert.Same(testLogValues, warning.State); Assert.Equal(0, warning.EventId); Assert.Equal(null, warning.Exception); Assert.Equal("Test 1", warning.Formatter(warning.State, warning.Exception)); var error = sink.Writes[3]; Assert.Equal(LogLevel.Error, error.LogLevel); Assert.Same(testLogValues, error.State); Assert.Equal(0, error.EventId); Assert.Equal(null, error.Exception); Assert.Equal("Test 1", error.Formatter(error.State, error.Exception)); var critical = sink.Writes[4]; Assert.Equal(LogLevel.Critical, critical.LogLevel); Assert.Same(testLogValues, critical.State); Assert.Equal(0, critical.EventId); Assert.Equal(null, critical.Exception); Assert.Equal("Test 1", critical.Formatter(critical.State, critical.Exception)); var debug = sink.Writes[5]; Assert.Equal(LogLevel.Debug, debug.LogLevel); Assert.Same(testLogValues, debug.State); Assert.Equal(0, debug.EventId); Assert.Equal(null, debug.Exception); Assert.Equal("Test 1", debug.Formatter(debug.State, debug.Exception)); }
public TestLogger(string name, TestSink sink, Func <LogLevel, bool> filter) { _sink = sink; _name = name; _filter = filter; }
public TestLogger(string name, TestSink sink, bool enabled) : this(name, sink, _ => enabled) { }
public void LogValuesEventIdAndError_LogsCorrectValues() { // Arrange var sink = new TestSink(); var logger = SetUp(sink); var testLogValues = new TestLogValues() { Value = 1 }; // Act logger.LogTrace(1, _exception, testLogValues.ToString()); logger.LogInformation(2, _exception, testLogValues.ToString()); logger.LogWarning(3, _exception, testLogValues.ToString()); logger.LogError(4, _exception, testLogValues.ToString()); logger.LogCritical(5, _exception, testLogValues.ToString()); logger.LogDebug(6, _exception, testLogValues.ToString()); // Assert Assert.Equal(6, sink.Writes.Count()); Assert.True(sink.Writes.TryTake(out var trace)); Assert.Equal(LogLevel.Trace, trace.LogLevel); Assert.Equal(testLogValues.ToString(), trace.State.ToString()); Assert.Equal(1, trace.EventId); Assert.Equal(_exception, trace.Exception); Assert.Equal( "Test 1", trace.Formatter(trace.State, trace.Exception)); Assert.True(sink.Writes.TryTake(out var information)); Assert.Equal(LogLevel.Information, information.LogLevel); Assert.Equal(testLogValues.ToString(), information.State.ToString()); Assert.Equal(2, information.EventId); Assert.Equal(_exception, information.Exception); Assert.Equal( "Test 1", information.Formatter(information.State, information.Exception)); Assert.True(sink.Writes.TryTake(out var warning)); Assert.Equal(LogLevel.Warning, warning.LogLevel); Assert.Equal(testLogValues.ToString(), warning.State.ToString()); Assert.Equal(3, warning.EventId); Assert.Equal(_exception, warning.Exception); Assert.Equal( "Test 1", warning.Formatter(warning.State, warning.Exception)); Assert.True(sink.Writes.TryTake(out var error)); Assert.Equal(LogLevel.Error, error.LogLevel); Assert.Equal(testLogValues.ToString(), error.State.ToString()); Assert.Equal(4, error.EventId); Assert.Equal(_exception, error.Exception); Assert.Equal( "Test 1", error.Formatter(error.State, error.Exception)); Assert.True(sink.Writes.TryTake(out var critical)); Assert.Equal(LogLevel.Critical, critical.LogLevel); Assert.Equal(testLogValues.ToString(), critical.State.ToString()); Assert.Equal(5, critical.EventId); Assert.Equal(_exception, critical.Exception); Assert.Equal( "Test 1", critical.Formatter(critical.State, critical.Exception)); Assert.True(sink.Writes.TryTake(out var debug)); Assert.Equal(LogLevel.Debug, debug.LogLevel); Assert.Equal(testLogValues.ToString(), debug.State.ToString()); Assert.Equal(6, debug.EventId); Assert.Equal(_exception, debug.Exception); Assert.Equal( "Test 1", debug.Formatter(debug.State, debug.Exception)); }
public TestLoggerProvider(TestSink testSink, Func <LogLevel, bool> filter) { Sink = testSink; _filter = filter; }
public TestLoggerProvider(TestSink testSink, bool isEnabled) : this(testSink, _ => isEnabled) { }
public void LogValuesAndError_LogsCorrectValues() { // Arrange var sink = new TestSink(); var logger = SetUp(sink); var testLogValues = new TestLogValues() { Value = 1 }; // Act logger.LogTrace(0, _exception, testLogValues.ToString()); logger.LogInformation(0, _exception, testLogValues.ToString()); logger.LogWarning(0, _exception, testLogValues.ToString()); logger.LogError(0, _exception, testLogValues.ToString()); logger.LogCritical(0, _exception, testLogValues.ToString()); logger.LogDebug(0, _exception, testLogValues.ToString()); // Assert Assert.Equal(6, sink.Writes.Count); var trace = sink.Writes[0]; Assert.Equal(LogLevel.Trace, trace.LogLevel); Assert.Equal(0, trace.EventId); Assert.Equal(_exception, trace.Exception); Assert.Equal( "Test 1", trace.Formatter(trace.State, trace.Exception)); var information = sink.Writes[1]; Assert.Equal(LogLevel.Information, information.LogLevel); Assert.Equal(0, information.EventId); Assert.Equal(_exception, information.Exception); Assert.Equal( "Test 1", information.Formatter(information.State, information.Exception)); var warning = sink.Writes[2]; Assert.Equal(LogLevel.Warning, warning.LogLevel); Assert.Equal(0, warning.EventId); Assert.Equal(_exception, warning.Exception); Assert.Equal( "Test 1", warning.Formatter(warning.State, warning.Exception)); var error = sink.Writes[3]; Assert.Equal(LogLevel.Error, error.LogLevel); Assert.Equal(0, error.EventId); Assert.Equal(_exception, error.Exception); Assert.Equal( "Test 1", error.Formatter(error.State, error.Exception)); var critical = sink.Writes[4]; Assert.Equal(LogLevel.Critical, critical.LogLevel); Assert.Equal(0, critical.EventId); Assert.Equal(_exception, critical.Exception); Assert.Equal( "Test 1", critical.Formatter(critical.State, critical.Exception)); var debug = sink.Writes[5]; Assert.Equal(LogLevel.Debug, debug.LogLevel); Assert.Equal(0, debug.EventId); Assert.Equal(_exception, debug.Exception); Assert.Equal( "Test 1", debug.Formatter(debug.State, debug.Exception)); }
public void MessageOnly_LogsCorrectValues() { // Arrange var sink = new TestSink(); var logger = SetUp(sink); // Act logger.LogTrace(_state); logger.LogInformation(_state); logger.LogWarning(_state); logger.LogError(_state); logger.LogCritical(_state); logger.LogDebug(_state); logger.Log(LogLevel.Information, _state); // Assert Assert.Equal(7, sink.Writes.Count); var trace = sink.Writes[0]; Assert.Equal(LogLevel.Trace, trace.LogLevel); Assert.Equal(_state, trace.State.ToString()); Assert.Equal(0, trace.EventId); Assert.Null(trace.Exception); var information = sink.Writes[1]; Assert.Equal(LogLevel.Information, information.LogLevel); Assert.Equal(_state, information.State.ToString()); Assert.Equal(0, information.EventId); Assert.Null(information.Exception); var warning = sink.Writes[2]; Assert.Equal(LogLevel.Warning, warning.LogLevel); Assert.Equal(_state, warning.State.ToString()); Assert.Equal(0, warning.EventId); Assert.Null(warning.Exception); var error = sink.Writes[3]; Assert.Equal(LogLevel.Error, error.LogLevel); Assert.Equal(_state, error.State.ToString()); Assert.Equal(0, error.EventId); Assert.Null(error.Exception); var critical = sink.Writes[4]; Assert.Equal(LogLevel.Critical, critical.LogLevel); Assert.Equal(_state, critical.State.ToString()); Assert.Equal(0, critical.EventId); Assert.Null(critical.Exception); var debug = sink.Writes[5]; Assert.Equal(LogLevel.Debug, debug.LogLevel); Assert.Equal(_state, debug.State.ToString()); Assert.Equal(0, debug.EventId); Assert.Null(debug.Exception); var logInf = sink.Writes[6]; Assert.Equal(LogLevel.Information, logInf.LogLevel); Assert.Equal(_state, logInf.State.ToString()); Assert.Equal(0, logInf.EventId); Assert.Null(logInf.Exception); }
public TestLoggerProvider2(TestSink testSink) : base(testSink, true) { }
private TestLogger SetUp(TestSink sink) { // Arrange var logger = new TestLogger(_name, sink, enabled: true); return logger; }
public TestLogger(string name, TestSink sink, bool enabled) { _sink = sink; _name = name; _enabled = enabled; }
public TestLoggerFactory(TestSink sink, bool enabled) { _sink = sink; _enabled = enabled; }