I find myself writing methods with try{stuff}catch(Exception e){log, other stuff} quit a bit, so I was trying to figure out how to make an attribute to help me out. I've checked out the following threads pretty extensively, and can't seem to get my implementation to work.
attribute does not seem to act at all
ASP.NET MVC Controller.OnException not being called
.net Attributes that handle exceptions - usage on a property accessor
My top-level web.config is set to
<customErrors mode="On" defaultRedirect="/error.html"/>
and I'm compiling in not-debug mode. My attribute is below :
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class SafeWebMethodAttribute: ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ThrowIfNull();
if (filterContext.ExceptionHandled)
{
return;
}
Log.LogException(filterContext.Exception);
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.Write(filterContext.Exception);
filterContext.ExceptionHandled = true;
}
}
and I'm calling it here -
public class Test : Controller { [SafeWebMethod] public ActionResult Test() { throw new ArgumentException("test"); } }
I can't seem to get a breakpoint hit in the attribute, or if I change the status code to get it to show up.
I've also copied the code from the [HandleError] attribute, and can't get a breakpoint in there either, so I think it is something wrong with my configuration, but I can't figure out what.
Any thoughts or ideas would be greatly appreciated