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