Пример #1
0
        public async void Invoke_DoesNotLogWhenDisabledAndHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: false);

            var mockContext = new Mock <HttpContext>(MockBehavior.Strict);

            mockContext.Setup(m => m.RequestServices.GetService(typeof(ILoggerFactory)))
            .Returns(loggerFactory);

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router = new TestRouter(isHandled);
            var mockServiceProvider = new Mock <IServiceProvider>();
            var middleware          = new RouterMiddleware(next, mockServiceProvider.Object, router);

            // Act
            await middleware.Invoke(mockContext.Object);

            // Assert
            // exists a BeginScope, verify contents
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Empty(sink.Writes);
        }
Пример #2
0
        public async void Invoke_DoesNotLogWhenDisabledAndHandled()
        {
            // Arrange
            var isHandled = true;

            var sink = new TestSink(
                TestSink.EnableWithTypeName <RouterMiddleware>,
                TestSink.EnableWithTypeName <RouterMiddleware>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: false);

            var httpContext = new DefaultHttpContext();

            httpContext.ApplicationServices = new ServiceProvider();
            httpContext.RequestServices     = httpContext.ApplicationServices;

            RequestDelegate next = (c) =>
            {
                return(Task.FromResult <object>(null));
            };

            var router     = new TestRouter(isHandled);
            var middleware = new RouterMiddleware(next, httpContext.ApplicationServices, loggerFactory, router);

            // Act
            await middleware.Invoke(httpContext);

            // Assert
            // exists a BeginScope, verify contents
            Assert.Single(sink.Scopes);
            var scope = sink.Scopes[0];

            Assert.Equal(typeof(RouterMiddleware).FullName, scope.LoggerName);
            Assert.Equal("RouterMiddleware.Invoke", scope.Scope);

            Assert.Empty(sink.Writes);
        }