public async Task When_SendingAsyncRespondsWithStatusCode_Expect_LogLevelCritical() { // Arrange Mock <ILogger <RequestLoggingHttpMessageHandler> > mock = new Mock <ILogger <RequestLoggingHttpMessageHandler> >(); mock.Setup(m => m.IsEnabled(It.IsAny <LogLevel>())).Returns(true); Exception exception = new Exception(); RequestLoggingHttpMessageHandler handler = new RequestLoggingHttpMessageHandler(mock.Object) { InnerHandler = new MockExceptionThrowingInnerHandler(exception), }; Exception result; // Act using (HttpClient client = new HttpClient(handler)) { result = await Record.ExceptionAsync(() => client.GetAsync(new Uri("http://localhost"))); } // Assert mock.Verify( m => m.Log( LogLevel.Information, It.IsAny <EventId>(), It.Is <It.IsAnyType>((@object, type) => @object.ToString() !.Equals("Outbound HTTP GET http://localhost/ started")), null, (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once); mock.Verify( m => m.Log( LogLevel.Critical, It.IsAny <EventId>(), It.Is <It.IsAnyType>((@object, type) => @object.ToString() !.Equals("Outbound HTTP GET http://localhost/ finished - Unhandled Exception")), exception, (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once); Assert.NotNull(result); Assert.Equal(exception, result); }
public async Task When_SendingAsyncRespondsWithStatusCode_Expect_LogLevel(int statusCode, LogLevel expectedLogLevel) { // Arrange Mock <ILogger <RequestLoggingHttpMessageHandler> > mock = new Mock <ILogger <RequestLoggingHttpMessageHandler> >(); mock.Setup(m => m.IsEnabled(It.IsAny <LogLevel>())).Returns(true); RequestLoggingHttpMessageHandler handler = new RequestLoggingHttpMessageHandler(mock.Object) { InnerHandler = new MockResponseReturningInnerHandler(statusCode), }; // Act using (HttpClient client = new HttpClient(handler)) { await client.GetAsync(new Uri("http://localhost")); } // Assert mock.Verify( m => m.Log( LogLevel.Information, It.IsAny <EventId>(), It.Is <It.IsAnyType>((@object, type) => @object.ToString() !.Equals("Outbound HTTP GET http://localhost/ started")), null, (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once()); mock.Verify( m => m.Log( expectedLogLevel, It.IsAny <EventId>(), It.Is <It.IsAnyType>((@object, type) => @object.ToString() !.Equals($"Outbound HTTP GET http://localhost/ finished - {statusCode}")), null, (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once()); }