示例#1
0
        public void 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;
            };

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

            policy.Execute(() => HttpStatusCode.OK);

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
            resultInvokedFor.Should().BeNull();
        }
示例#2
0
        [Fact] public void 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;
            };

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

            var returnedResult = HttpStatusCode.InternalServerError;

            policy.Execute(() => returnedResult);

            invokedLogger.Should().Be(expectedLogger);
            exceptionInvokedFor.Should().BeNull();
            resultInvokedFor.Should().Be(returnedResult);
        }
示例#3
0
        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;
            };

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

            policy.Execute(() => { });

            invokedLogger.Should().BeNull();
            exceptionInvokedFor.Should().BeNull();
        }