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(); }
[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); }
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(); }