Last post Jun 24, 2010 11:57 PM by Coool
Dec 05, 2007 05:24 PM|diadem_2k|LINK
I new to this but very much interested in implementing exception handling mechanism in my asp.net 2.0 web application.
I want to do in a such that whatever error that happens, i should be sending in an e-mail without always writing try-catch.
i read many articles but i am missing something in each and every point, let me tell what i did till now:
1. in my web.config, i wrote <customErrors mode="On" defaultRedirect="UnExpectedError.aspx">
2. in my Unexpectederror.aspx page i want to catch the exception like:
Exception ex = HttpContext.Current.Server.GetLastError(); so that i can send e-mail to corresponding parties.
but when i try to access "ex" from UnexpectedError.aspx.cs file it says NULL and need to implement IHTTPModule.
then i read some article
now can anyone tell me complete procedure how can i do exception handling by combining all these pieces together...
this will help me a lot
thanks a lot in advance
Dec 05, 2007 05:36 PM|DavidKiff|LINK
Yes IHttpModule is one way a quicker and easier way is to use the global.asax file and use the OnError part:) like this:
void Application_Error(object sender, EventArgs e)
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError().GetBaseException();
Dec 05, 2007 08:48 PM|diadem_2k|LINK
if this is the simple way??? then why i am seeing so many articles on HTTPmodules??
what's the advantage of using HTTPModule method when we has application_error event handler method ??
Dec 06, 2007 06:32 AM|DavidKiff|LINK
yes it is the simple way! Other wise would will have to create a new class, implement IHttpModule, wire up to the appropriate event then Register this in Web.config file.
or you could open the global asax file and code!
Im not sure of the ins-and-outs but I believe its more scalable and to use a module. When created it can be plugged into other projects easily... however it requires more skill to create.
I could help you create one if you really want to go that way though...?
Dec 06, 2007 06:39 AM|DavidKiff|LINK
Here is the module:
then add this in your web.config file:
Dec 06, 2007 10:56 AM|diadem_2k|LINK
I did exactly what you told and it works excellent....i can send all the exception to my mail (taking inner exception from EX)
thanks a lot dude
Dec 06, 2007 10:58 AM|DavidKiff|LINK
No problem im glad it works :)
Jun 22, 2010 01:40 PM|nut666|LINK
I generate a unique ID after logging the exception to various media types (db, file, email etc). How do I return it to the default custom error page specified in
<customErrors > tag so that the ID is exposed to the user ?
Jun 24, 2010 11:57 PM|Coool|LINK
If you are interesting in logging and sending errors to mail then you can use free error handling module like Elmah. It is one of the most powerful error handling module available.
I am sure you would like it.