How to log an exception without a controller in asp.net mvc 3 when using customErrors mode="on"?
I've read a lot of material today on the topic of implementing custom error pages in ASP.NET MVC (using 3 RC2). One thing I want to be able to do is log exceptions, but I'm not sure where the best hook is.
I mainly followed Barry Dahlberg's advice which helped me set up the desired 404 behaviors. I want to keep those separate from server errors, and log them differently.
It seems that when I have <customErrors mode="On">, MVC3 just uses the view defined at ~/Views/Shared/Error.cshtml, passing the correct System.Web.Mvc.HandleErrorInfo model instance. In effect, this is bypassing the controller where I would put the error logging code.
I tried setting <customErrors mode="On" defaultRedirect="errors/unexpected" />, but the controller action method never gets invoked.
The HandleErrorInfo model in the view has the exception information I need for logging. I could log the error in the view, but that开发者_如何转开发 smells funny.
Another solution would be to keep <customErrors mode="Off" />. Since this causes Application_Error to be invoked from Global.asax, I could log there instead.
Ideas?
You could write a custom HandleError attribute where you would log the exception in the overriden OnException method.
精彩评论