public void LoggingFilter_NullLogger_SetsHttpResponse() { var target = new LoggingRoutingHandleErrorAttribute(null); target.OnException(_exceptionContext); _responseMock.VerifySet(x => x.StatusCode = 500, Times.Once); Assert.AreEqual("Error", ((ViewResult)_exceptionContext.Result).ViewName); }
public void LoggingFilter_NullLogger_HandlesException() { var target = new LoggingRoutingHandleErrorAttribute(null); target.OnException(_exceptionContext); Assert.IsTrue(_exceptionContext.ExceptionHandled); // No exception thrown attempting to log }
public void LoggingFilter_Normal_HandlesException() { Mock <ILogger> logger = new Mock <ILogger>(); var target = new LoggingRoutingHandleErrorAttribute(logger.Object); target.OnException(_exceptionContext); Assert.IsTrue(_exceptionContext.ExceptionHandled); logger.Verify(l => l.Error(_exceptionContext.Exception.Message, _exceptionContext.Exception), Times.Exactly(1), "Logger was not called with the unhandled exception."); }
public void LoggingFilter_ExceptionIsAlreadyHandled_ErrorHttpResponseIsNotSet() { _exceptionContext.ExceptionHandled = true; Mock <ILogger> logger = new Mock <ILogger>(); var target = new LoggingRoutingHandleErrorAttribute(logger.Object); target.OnException(_exceptionContext); // Http Response is not set to display an error. Assert.IsNull(_exceptionContext.Result as ViewResult); _responseMock.VerifySet(x => x.StatusCode = 500, Times.Never); }
public void LoggingFilter_ExceptionIsAlreadyHandled_ExceptionIsNotHandledAgain() { _exceptionContext.ExceptionHandled = true; Mock <ILogger> logger = new Mock <ILogger>(); var target = new LoggingRoutingHandleErrorAttribute(logger.Object); target.OnException(_exceptionContext); // Exception is not un-handled or logged Assert.IsTrue(_exceptionContext.ExceptionHandled); logger.Verify(l => l.Error(_exceptionContext.Exception.Message, _exceptionContext.Exception), Times.Exactly(0), "Logger was called despite not handling the error."); }
protected void Application_Start() { // AreaRegistration.RegisterAllAreas(); // WebApiConfig.Register(GlobalConfiguration.Configuration); // BundleConfig.RegisterBundles(BundleTable.Bundles); // AuthConfig.RegisterAuth(); // Alternatively, we may want to hold a reference to this in AdPortal.dll, // so that we can be certain that all projects in this solution // future and current use the same logging implementation. GlobalLogger = new MediaRadarLoggerMock(); var unhandledErrorAttr = new LoggingRoutingHandleErrorAttribute(GlobalLogger); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters, new object[] { unhandledErrorAttr }); RouteConfig.RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(new AdPortalControllerFactory()); }