public void PolicyDoesNotCallTheLoggerIfPolicyDoesNotHandleException()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger        invokedLogger       = null;
            Exception      exceptionInvokedFor = null;
            HttpStatusCode?resultInvokedFor    = null;
            Action <ILogger, Context, DelegateResult <HttpStatusCode> > logAction = (logger, context, outcome) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = outcome.Exception;
                resultInvokedFor    = outcome.Result;
            };

            AsyncLoggingPolicy <HttpStatusCode> policy = Policy <HttpStatusCode>
                                                         .Handle <TaskCanceledException>()
                                                         .OrResult(r => r != HttpStatusCode.OK)
                                                         .AsyncLog(loggerProvider, logAction);

            var thrownException = new InvalidOperationException();

            policy.Awaiting(p => p.ExecuteAsync(() => throw thrownException)).ShouldThrow <InvalidOperationException>();

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
            resultInvokedFor.Should().BeNull();
        }
        public async Task PolicyCallsTheLoggerIfPolicyHandlesResult()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger        invokedLogger       = null;
            Exception      exceptionInvokedFor = null;
            HttpStatusCode?resultInvokedFor    = null;
            Action <ILogger, Context, DelegateResult <HttpStatusCode> > logAction = (logger, context, outcome) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = outcome.Exception;
                resultInvokedFor    = outcome.Result;
            };

            AsyncLoggingPolicy <HttpStatusCode> policy = Policy <HttpStatusCode>
                                                         .Handle <TaskCanceledException>()
                                                         .OrResult(r => r != HttpStatusCode.OK)
                                                         .AsyncLog(loggerProvider, logAction);

            var returnedResult = HttpStatusCode.InternalServerError;
            await policy.ExecuteAsync(() => Task.FromResult(returnedResult));

            invokedLogger.Should().Be(expectedLogger);
            exceptionInvokedFor.Should().BeNull();
            resultInvokedFor.Should().Be(returnedResult);
        }
        public async Task PolicyDoesNotCallTheLoggerIfSuccessfulExecution()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger        invokedLogger       = null;
            Exception      exceptionInvokedFor = null;
            HttpStatusCode?resultInvokedFor    = null;
            Action <ILogger, Context, DelegateResult <HttpStatusCode> > logAction = (logger, context, outcome) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = outcome.Exception;
                resultInvokedFor    = outcome.Result;
            };

            AsyncLoggingPolicy <HttpStatusCode> policy = Policy <HttpStatusCode>
                                                         .Handle <TaskCanceledException>()
                                                         .OrResult(r => r != HttpStatusCode.OK)
                                                         .AsyncLog(loggerProvider, logAction);

            await policy.ExecuteAsync(() => Task.FromResult(HttpStatusCode.OK));

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
            resultInvokedFor.Should().BeNull();
        }
Esempio n. 4
0
        public void PolicyCallsTheLoggerIfPolicyHandlesException()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger        invokedLogger       = null;
            Exception      exceptionInvokedFor = null;
            HttpStatusCode?resultInvokedFor    = null;
            Action <ILogger, Context, DelegateResult <HttpStatusCode> > logAction = (logger, context, outcome) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = outcome.Exception;
                resultInvokedFor    = outcome.Result;
            };

            LoggingPolicy <HttpStatusCode> policy = Policy <HttpStatusCode>
                                                    .Handle <TaskCanceledException>()
                                                    .OrResult(r => r != HttpStatusCode.OK)
                                                    .Log(loggerProvider, logAction);

            var thrownException = new TaskCanceledException();

            policy.Invoking(p => p.Execute(() => throw thrownException)).ShouldThrow <TaskCanceledException>();

            invokedLogger.Should().Be(expectedLogger);
            exceptionInvokedFor.Should().Be(thrownException);
            resultInvokedFor.Should().Be(default(HttpStatusCode));
        }
        public void PolicyDoesNotCallTheLoggerIfSuccessfulExecution()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger   invokedLogger       = null;
            Exception exceptionInvokedFor = null;
            Action <ILogger, Context, Exception> logAction = (logger, context, exception) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = exception;
            };

            AsyncLoggingPolicy policy = Policy.Handle <TimeoutException>().AsyncLog(loggerProvider, logAction);

            policy.ExecuteAsync(() => Task.CompletedTask);

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
        }
        public void PolicyDoesNotCallTheLoggerIfPolicyDoesNotHandleException()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger   invokedLogger       = null;
            Exception exceptionInvokedFor = null;
            Action <ILogger, Context, Exception> logAction = (logger, context, exception) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = exception;
            };

            AsyncLoggingPolicy policy = Policy.Handle <TimeoutException>().AsyncLog(loggerProvider, logAction);

            var thrownException = new InvalidOperationException();

            policy.Awaiting(p => p.ExecuteAsync(() => throw thrownException)).ShouldThrow <InvalidOperationException>();

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
        }
Esempio n. 7
0
        public void PolicyCallsTheLoggerIfPolicyHandlesException()
        {
            ILogger expectedLogger = new StubLogger();
            Func <Context, ILogger> loggerProvider = _ => expectedLogger;

            ILogger   invokedLogger       = null;
            Exception exceptionInvokedFor = null;
            Action <ILogger, Context, Exception> logAction = (logger, context, exception) =>
            {
                invokedLogger       = logger;
                exceptionInvokedFor = exception;
            };

            LoggingPolicy policy = Policy.Handle <TimeoutException>().Log(loggerProvider, logAction);

            var thrownException = new TimeoutException();

            policy.Invoking(p => p.Execute(() => throw thrownException)).ShouldThrow <TimeoutException>();

            invokedLogger.Should().Be(expectedLogger);
            exceptionInvokedFor.Should().Be(thrownException);
        }