static int Main(string[] args) { var session = new ConsoleSession(); var jobFactory = SelectFactory(ref args); if (jobFactory == null) { Console.Error.WriteLine($@"Usage: {Path.GetFileName(session.Application)} <mode> <args...> where <mode> is one of: inspect, import, create-datasource"); return(1); } session.Options.AddCollector(jobFactory); session.ArgumentList.AddCollector(jobFactory as IReceiveArgumentList); var logging = session.Options.AddCollector(new SimpleConsoleLoggingPolicy()); session.ExcessPositionalArgumentsPolicy = ExcessPositionalArgumentsPolicy.Warn; return(session.Run(args, async() => { using (LoggingPolicy.Register(session, logging)) { var job = jobFactory.CreateJob(); await job.Run(Console.Out); return 0; } })); }
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; }; LoggingPolicy <HttpStatusCode> policy = Policy <HttpStatusCode> .Handle <TaskCanceledException>() .OrResult(r => r != HttpStatusCode.OK) .Log(loggerProvider, logAction); var thrownException = new InvalidOperationException(); policy.Invoking(p => p.Execute(() => throw thrownException)).ShouldThrow <InvalidOperationException>(); 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 InitLogging WithPolicy(Action <ILoggingPolicyFor> action) { var policy = new LoggingPolicy(); action(policy); this.policies.Add(policy); return(this); }
public void ResetToDefault() { lock (LOCK) { this.logger = null; this.loggingPolicy = default(LoggingPolicy); this.errorHandlingPolicy = default(ErrorHandlingPolicy); } }
public void IsDefined_When_Misdefined(string path) { // Arrange var loggingPolicy = new LoggingPolicy { Path = path }; // Act // Assert loggingPolicy.IsDefined().Should().BeFalse(); }
public void IsDefined_When_Well_Defined(string section, string path) { // Arrange var loggingPolicy = new LoggingPolicy { Section = section, Path = path }; // Act // Assert loggingPolicy.IsDefined().Should().BeTrue(); }
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(); }
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; }; LoggingPolicy policy = Policy.Handle <TimeoutException>().Log(loggerProvider, logAction); var thrownException = new InvalidOperationException(); policy.Invoking(p => p.Execute(() => throw thrownException)).ShouldThrow <InvalidOperationException>(); invokedLogger.Should().BeNull(); exceptionInvokedFor.Should().BeNull(); }