I'm building an MVC 3.0 ecommerce site. I want to avoid sticking try-catch blocks all over my code.
Question is pretty simple - What is a good strategy for capturing all errors that are thrown on my website? I want to know about all of them... so I can work on bringing that count to 0 once the site goes live. I plan on writing each error to a logger and also to email it.
I've done some reading about capturing it in the global.asax file... but i've also read that it doesn't get ALL of them.
Anyone have any suggestions or can point me in a good direction?
You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.
An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.
Generally speaking, error handling in ASP.NET MVC is mainly necessary to handle program and route exceptions. Program exceptions refer to catching errors in controllers and in any code you may have in Razor views.
Navigating to /home/contact in the browser, and you will see the following yellow page (also known as the Yellow Screen of Death) that shows exception details such as exception type, line number and file name where the exception occurred, and stack trace. Default Error Page in MVC ASP.NET provides the following ways to handle exceptions:
The catch and block method is considered the traditional method used for exception handling in MVC based applications. You will be logging the error information using this method. You enter the exception condition in the try blog and add the exception name for the catch block.
This can also be used to handle and log all the errors within the application. This method allows you to override the OnException method within the controller class. The error code 500 is used to handle all the logged errors resulting from the exception handling in MVC.
The MVC way of solving that problem are filters. If you are on MVC3 already, global filters are for you. There is special type of filter for handing errors: the HandleError
filter. This article describes much of using it.
You might implement you own filter and control every aspect of of unhandled exceptions, e.g.:
public class HandleExceptionsAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { var expception = filterContext.Exception; // custom code here.. } }
Finally, if you want to have all unhandled exceptions to be stored and logged, ELMAH i.e. ELMAH.MVC is the best choice. Install it by using the NuGet package, configure it and all data will be accessed from a link such as:
http://yourapp/admin/elmah/
My suggestion is to not use anything like Application_Error
in Global.asax.cs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With