public void LogEntriesIdentifyAllRecordedScopes() { var firstScopeState = Guid.NewGuid().ToString(); var secondScopeState = Guid.NewGuid().ToString(); var message = Guid.NewGuid().ToString(); using var sut = new CacheLogger(); sut.LogInformation("Before any scopes"); using (sut.BeginScope(firstScopeState)) { sut.LogInformation(message); using (sut.BeginScope(secondScopeState)) { sut.LogInformation(message); } } var entries = sut.Entries.ToList(); entries.Should().HaveCount(3); entries[0].Scopes.Should().BeEmpty(); entries[1].Scopes.Should().HaveCount(1); entries[1].Scopes.First().Should().Be(firstScopeState); entries[2].Scopes.Should().HaveCount(2); entries[2].Scopes.First().Should().Be(secondScopeState); entries[2].Scopes.Skip(1).First().Should().Be(firstScopeState); }
public void LogDoesNotLogRecordWhenIsEnabledReturnsFalse() { const LogLevel logLevel = LogLevel.Error; var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var data = Guid.NewGuid().ToString(); var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); string Formatter(string message, Exception?error) => data; var source = Substitute.For <ILogger>(); source.IsEnabled(logLevel).Returns(false); using var sut = new CacheLogger(source); sut.Log(logLevel, eventId, state, exception, Formatter); source.DidNotReceive() .Log(Arg.Any <LogLevel>(), Arg.Any <EventId>(), Arg.Any <string>(), Arg.Any <Exception>(), Arg.Any <Func <string, Exception, string> >()); sut.Entries.Should().BeEmpty(); sut.Last.Should().BeNull(); }
public void CacheIsEmptyWhenNoLogEntriesWritten() { using var sut = new CacheLogger(); sut.Count.Should().Be(0); sut.Entries.Should().BeEmpty(); sut.Last.Should().BeNull(); }
public void CanDisposeWithoutFactory() { using var sut = new CacheLogger(); // ReSharper disable once AccessToDisposedClosure Action action = () => sut.Dispose(); action.Should().NotThrow(); }
public void BeginScopeReturnsInstanceWhenNoSourceLoggerProvided() { var state = Guid.NewGuid().ToString(); using var sut = new CacheLogger(); using var actual = sut.BeginScope(state); actual.Should().NotBeNull(); }
public void IsEnabledReturnsTrueWithNullSourceLogger() { const LogLevel level = LogLevel.Error; using var sut = new CacheLogger(); var actual = sut.IsEnabled(level); actual.Should().BeTrue(); }
public void DisposeCallsDisposeOnFactory() { var logger = Substitute.For <ILogger>(); var factory = Substitute.For <ILoggerFactory>(); using var sut = new CacheLogger(logger, factory); sut.Dispose(); factory.Received().Dispose(); }
public void LogIgnoresNullFormattedMessage() { var exception = new TimeoutException(); using var sut = new CacheLogger(); sut.LogInformation(exception, null); sut.Count.Should().Be(1); sut.Last !.Exception.Should().Be(exception); sut.Last.Message.Should().BeEmpty(); }
public void BeginScopeReturnsInstanceWhenSourceLoggerBeginScopeReturnsNull() { var state = Guid.NewGuid().ToString(); var logger = Substitute.For <ILogger>(); logger.BeginScope(state).Returns((IDisposable)null !); using var sut = new CacheLogger(logger); using var actual = sut.BeginScope(state); actual.Should().NotBeNull(); }
public void LogEntryContainsSnapshotOfActiveScopes() { using var sut = new CacheLogger(); var state = Guid.NewGuid(); using (sut.BeginScope(state)) { sut.LogDebug(Guid.NewGuid().ToString()); } sut.Last !.Scopes.Single().Should().Be(state); }
public void IsEnabledReturnsSourceLoggerIsEnabledTest(bool expected) { const LogLevel level = LogLevel.Error; var source = Substitute.For <ILogger>(); source.IsEnabled(level).Returns(expected); using var sut = new CacheLogger(source); var actual = sut.IsEnabled(level); actual.Should().Be(expected); }
public void LastLogEntryCapturesScopeStateWhenNoLoggerProvided() { var state = Guid.NewGuid().ToString(); var message = Guid.NewGuid().ToString(); using var sut = new CacheLogger(); using (sut.BeginScope(state)) { sut.LogInformation(message); } sut.Last !.Scopes.Should().HaveCount(1); sut.Last.Scopes.Single().Should().Be(state); }
public void LogThrowsExceptionWithNullFormatter() { const LogLevel logLevel = LogLevel.Error; var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); var logger = Substitute.For <ILogger>(); using var sut = new CacheLogger(logger); // ReSharper disable once AccessToDisposedClosure Action action = () => sut.Log(logLevel, eventId, state, exception, null !); action.Should().Throw <ArgumentNullException>(); }
public void BeginScopeReturnsWrappedInstanceWhenSourceLoggerProvided() { var state = Guid.NewGuid().ToString(); var source = Substitute.For <ILogger>(); var scope = Substitute.For <IDisposable>(); source.BeginScope(state).Returns(scope); using var sut = new CacheLogger(source); var actual = sut.BeginScope(state); actual.Should().NotBeNull(); actual.Should().NotBeSameAs(scope); }
public void LastLogEntryCapturesScopeStateWhenSourceLoggerBeginScopeReturnsNull() { var state = Guid.NewGuid().ToString(); var message = Guid.NewGuid().ToString(); var logger = Substitute.For <ILogger>(); logger.IsEnabled(Arg.Any <LogLevel>()).Returns(true); logger.BeginScope(state).Returns((IDisposable)null !); using var sut = new CacheLogger(logger); using (sut.BeginScope(state)) { sut.LogInformation(message); } sut.Last !.Scopes.Should().HaveCount(1); sut.Last.Scopes.Single().Should().Be(state); }
public void LastReturnsLogEntry() { const LogLevel logLevel = LogLevel.Error; var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var data = Guid.NewGuid().ToString(); var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); string Formatter(string message, Exception?error) => data; using var sut = new CacheLogger(); sut.Log(logLevel, eventId, state, exception, Formatter); sut.Last !.EventId.Should().Be(eventId); sut.Last.Exception.Should().Be(exception); sut.Last.LogLevel.Should().Be(logLevel); sut.Last.State.Should().Be(state); sut.Last.Message.Should().Be(data); }
public void LogSendsLogToLogger() { const LogLevel logLevel = LogLevel.Error; var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var data = Guid.NewGuid().ToString(); var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); string Formatter(string message, Exception?error) => data; var source = Substitute.For <ILogger>(); source.IsEnabled(logLevel).Returns(true); using var sut = new CacheLogger(source); sut.Log(logLevel, eventId, state, exception, Formatter); source.Received(1).Log(logLevel, eventId, state, exception, Formatter); }
public void LogDoesLogsRecordWhenFormatterReturnsMessageAndExceptionIsNull() { const LogLevel logLevel = LogLevel.Error; var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var data = Guid.NewGuid().ToString(); string Formatter(string message, Exception?error) => data; var source = Substitute.For <ILogger>(); source.IsEnabled(logLevel).Returns(true); using var sut = new CacheLogger(source); sut.Log(logLevel, eventId, state, null, Formatter); source.Received(1).Log(logLevel, eventId, state, null, Formatter); sut.Entries.Should().NotBeEmpty(); sut.Last.Should().NotBeNull(); }
public void LogCachesLogMessageTest(LogLevel logLevel) { var eventId = Model.Create <EventId>(); var state = Guid.NewGuid().ToString(); var data = Guid.NewGuid().ToString(); var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); string Formatter(string message, Exception?error) => data; using var sut = new CacheLogger(); sut.Log(logLevel, eventId, state, exception, Formatter); sut.Entries.Should().HaveCount(1); var entry = sut.Entries.Single(); entry.EventId.Should().Be(eventId); entry.Exception.Should().Be(exception); entry.LogLevel.Should().Be(logLevel); entry.State.Should().Be(state); entry.Message.Should().Be(data); }
public ILogger CreateLogger(string categoryName) { var result = new CacheLogger(); return(result); }