Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
 public TestLogger(string name, TestSink sink, bool enabled)
 {
     _sink    = sink;
     _name    = name;
     _enabled = enabled;
 }
 public TestLoggerFactory(TestSink sink, bool enabled)
 {
     _sink    = sink;
     _enabled = enabled;
 }