September 18, 2012
@ 07:49 PM
Just Implement a Controller Error Handler call "LogError":
  public class LogError : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            
           ILoggingService logService = DependencyResolver.Current.GetService<ILoggingService>();
          
           logService.Logging(filterContext.Exception.Message,1);

            //redirect to customer error page
            filterContext.Result =
               new RedirectToRouteResult(
                   new RouteValueDictionary{{ "controller", "WsunTest" },
                                                 { "action", "Error" },
                                                 {"message",filterContext.Exception.Message},
                                                 { "returnUrl",    filterContext.HttpContext.Request.RawUrl }
                                                });
            filterContext.ExceptionHandled = true;

        }
    }

And below is the controller, I put the filter on the top of Controller:
 [LogError]
    public class WsunTestController : Controller
    {
       
       public ActionResult Index()
       {
                throw  new Exception("test exception message");

            return View();
          
       }   
      

        public ActionResult Error(string message="unknow")
        {
            ViewBag.message=message;
            return View();
        }

}

There is another way is override the parent controller:
 protected override void OnException(ExceptionContext filterContext)
        {

            // Don't interfere if the exception is already handled
            if (filterContext.ExceptionHandled)
                return;
            //do something to handele the exception
            filterContext.ExceptionHandled = true;
}
Easy to understand..buy I think Filter or actionFilter is more flexible easy to use.