public void LogSendsWithPropertiesAndExtendedProperties() { LogEntry logEntry = null; var exception = new Exception("Some random exception"); var eventId = new EventId(15, "SomeEvent"); var state = new Dictionary <string, string>(); var loggerMock = new Mock <ILogger>(); loggerMock.Setup(lm => lm.Level).Returns(LogLevel.Debug); loggerMock .Setup(lm => lm.Log(It.IsAny <LogEntry>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())) .Callback <LogEntry, string, string, int>((le, s1, s2, i) => logEntry = le); var rlLogger = new RockLibLogger(loggerMock.Object, "SomeCategory"); rlLogger.Log(MSE.LogLevel.Debug, eventId, state, exception, (dictionary, ex) => "Simple message"); logEntry.Should().NotBeNull(); logEntry.Level.Should().Be(LogLevel.Debug); logEntry.Message.Should().Be("Simple message"); logEntry.Exception.Should().BeSameAs(exception); logEntry.ExtendedProperties["Microsoft.Extensions.Logging.EventId"].Should().Be(eventId); logEntry.ExtendedProperties["Microsoft.Extensions.Logging.State"].Should().Be(state); logEntry.ExtendedProperties["Microsoft.Extensions.Logging.CategoryName"].Should().Be("SomeCategory"); }
public void LogDoesNotAddEmptyScopeToExtendedProperties() { LogEntry logEntry = null; var exception = new Exception("Some random exception"); var eventId = new EventId(15, "SomeEvent"); var state = new Dictionary <string, string>(); var loggerMock = new Mock <ILogger>(); loggerMock.Setup(lm => lm.Level).Returns(LogLevel.Debug); loggerMock .Setup(lm => lm.Log(It.IsAny <LogEntry>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())) .Callback <LogEntry, string, string, int>((le, s1, s2, i) => logEntry = le); var rlLogger = new RockLibLogger(loggerMock.Object, "SomeCategory"); using (rlLogger.BeginScope("a")) using (rlLogger.BeginScope("b")) using (rlLogger.BeginScope("c")) { } rlLogger.Log(MSE.LogLevel.Debug, eventId, state, exception, (dictionary, ex) => "Simple message"); logEntry.ExtendedProperties.Should().NotContainKey("Microsoft.Extensions.Logging.Scope"); }
public void LogMethodHappyPath2() { var mockLogger = new MockLogger(LogLevel.Fatal); var scopeProvider = new TestScopeProvider { State = "MyState" }; var rockLibLogger = new RockLibLogger(mockLogger.Object, "MyCategory", scopeProvider); var ex = new Exception(); var eventId = new EventId(123); string capturedState = null; Exception capturedException = null; rockLibLogger.Log(Warning, eventId, "Hello, world!", ex, Format); mockLogger.VerifyWarn(Times.Never()); capturedState.Should().BeNull(); capturedException.Should().BeNull(); string Format(string state, Exception exception) { capturedState = state; capturedException = exception; return("formatted"); } }
public void LogThrowsWithNullFormatter() { var loggerMock = new Mock <ILogger>(); var rlLogger = new RockLibLogger(loggerMock.Object, null); Action action = () => rlLogger.Log(MSE.LogLevel.None, new EventId(1), new Dictionary <string, string>(), null, null); action.Should().Throw <ArgumentNullException>().WithMessage("Value cannot be null.\r\nParameter name: formatter"); }
public void LogDoesNothingWhenDisabled() { var loggerMock = new Mock <ILogger>(); loggerMock.Setup(lm => lm.IsDisabled).Returns(true); var rlLogger = new RockLibLogger(loggerMock.Object, null); rlLogger.Log(MSE.LogLevel.None, new EventId(1), new Dictionary <string, string>(), null, (dictionary, exception) => ""); loggerMock.Verify(lm => lm.IsDisabled, Times.Once); loggerMock.VerifyNoOtherCalls(); }
public void LogMethodHappyPath1() { var mockLogger = new MockLogger(LogLevel.Warn); var scopeProvider = new TestScopeProvider { State = "MyState" }; var rockLibLogger = new RockLibLogger(mockLogger.Object, "MyCategory", scopeProvider); var ex = new Exception(); var eventId = new EventId(123); string capturedState = null; Exception capturedException = null; rockLibLogger.Log(Warning, eventId, "Hello, world!", ex, Format); var extendedProperties = new Dictionary <string, object> { ["Microsoft.Extensions.Logging.EventId"] = eventId, ["Microsoft.Extensions.Logging.State"] = "^Hello, world!$", ["Microsoft.Extensions.Logging.CategoryName"] = "^MyCategory$", ["Microsoft.Extensions.Logging.Scope"] = new object[] { "^MyState$" } }; mockLogger.VerifyWarn("^formatted$", extendedProperties, Times.Once()); capturedState.Should().Be("Hello, world!"); capturedException.Should().BeSameAs(ex); string Format(string state, Exception exception) { capturedState = state; capturedException = exception; return("formatted"); } }