public async Task IfHandledClientError_ItLogsError() { // Arrange // Logger var loggerMock = new Mock <ILogger <GlobalLoggerMiddleware> >(); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())) .Returns(loggerMock.Object); // Diagnostic var diagnoticSourceMock = new Mock <DiagnosticSource>(); // Request var requestMock = new Mock <HttpRequest>(); requestMock.Setup(x => x.Scheme).Returns("http"); requestMock.Setup(x => x.Host).Returns(new HostString("localhost")); requestMock.Setup(x => x.Path).Returns(new PathString("/FooBar")); requestMock.Setup(x => x.PathBase).Returns(new PathString("/")); requestMock.Setup(x => x.Method).Returns("GET"); requestMock.Setup(x => x.Body).Returns(new MemoryStream()); // Response var responseMock = new Mock <HttpResponse>(); responseMock.SetupGet(y => y.StatusCode).Returns(499); // Context var contextMock = new Mock <HttpContext>(); contextMock.Setup(z => z.Request).Returns(requestMock.Object); contextMock.Setup(z => z.Response).Returns(responseMock.Object); // Middleware var logRequestMiddleware = new GlobalLoggerMiddleware(next: (innerHttpContext) => Task.FromResult(0), loggerFactory: loggerFactory.Object, diagnosticSource: diagnoticSourceMock.Object); // Act await logRequestMiddleware.Invoke(contextMock.Object); // Assert loggerMock.Verify(l => l.Log <object>( LogLevel.Error, It.IsAny <EventId>(), It.Is <object>(fV => fV.ToString().Equals(($"An error has occurred: 499"))), null, It.IsAny <Func <object, Exception, string> >())); }
public async Task IfHandledInternalServerError_ItLogsError() { // Arrange // Logger var loggerMock = new Mock <ILogger <GlobalLoggerMiddleware> >(); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())) .Returns(loggerMock.Object); // Diagnostic var diagnoticSourceMock = new Mock <DiagnosticSource>(); // Request var requestMock = new Mock <HttpRequest>(); requestMock.Setup(x => x.Scheme).Returns("http"); requestMock.Setup(x => x.Host).Returns(new HostString("localhost")); requestMock.Setup(x => x.Path).Returns(new PathString("/FooBar")); requestMock.Setup(x => x.PathBase).Returns(new PathString("/")); requestMock.Setup(x => x.Method).Returns("GET"); requestMock.Setup(x => x.Body).Returns(new MemoryStream()); // Response var responseMock = new Mock <HttpResponse>(); responseMock.SetupGet(y => y.StatusCode).Returns(500); // Context var features = new FeatureCollection(); var exMessage = "I'm just exceptional"; var exceptionHandlerFeature = new ExceptionHandlerFeature() { Error = new Exception(exMessage), }; features.Set <IExceptionHandlerFeature>(exceptionHandlerFeature); var contextMock = new Mock <HttpContext>(); contextMock.Setup(z => z.Request).Returns(requestMock.Object); contextMock.Setup(z => z.Response).Returns(responseMock.Object); contextMock.Setup(z => z.Features).Returns(features); // Middleware var logRequestMiddleware = new GlobalLoggerMiddleware(next: (innerHttpContext) => Task.FromResult(0), loggerFactory: loggerFactory.Object, diagnosticSource: diagnoticSourceMock.Object); // Act await logRequestMiddleware.Invoke(contextMock.Object); // Assert loggerMock.Verify(l => l.Log( LogLevel.Error, It.IsAny <EventId>(), It.Is <It.IsAnyType>((object fV, Type _) => fV.ToString().Equals(($"An internal handled exception has occurred: {exMessage}"))), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())); }