I am logging errors in my ASP.NET MVC 3's Application_Error method in the Global.asax file. My problem is that if customErrors in Web.config is set to true no logging happens. It does happen if it is set to Off. If an exception is thrown, Application_Error will always be called no matter the customError settings - or so I thought? What is up?
1 Answer
The possible values of the mode attribute are On, Off and RemoteOnly so I suspect that if you set it to true your application wouldn't even load.
On the other hand if you set mode="On" in case of an exception there is a global error filter in ASP.NET MVC 3 (see the RegisterGlobalFilters method in your Global.asax) which kicks in and automatically renders the ~/Views/Shared/Error.cshtml view => in this case the Application_Error method won't be called because the exception is handled.
2 Comments
Jacob Rohde
Yes I meant On and not true - sorry about that. So I guess the proper way would be to handle the logging in the OnException method in the base controller class instead of Application_Error. Hmm...thanks for that.
JasonMenny
HI, I,ve implemented Application_Error in Global.asax in order to invoke my ErrorsController. In my dev machine everything is working while in production I get always default error messages from the browser! Should I set CustomErrors to true?