using Global asax for error trapping

G

Guest

I need to be able to trap errors at the application level. I added this code
to the Global.asax file. The code I wrote is supposed to get the last error
that was generated and write to the event log as well as fire up my email
class and generate an email to send me the error. But no matter what I do, I
still get the icky looking yellow and white error screens.

I’ve turned on customErrors in the web.config but not default URL setting.
The pages don’t have Page_Error method in them so I would except the error to
move next up in the hierarchy which would be Application_Error. There are so
many pages in this project, I really cant trap general errors at the page
level. What am I doing wrong?????

protected void Application_Error(Object sender, EventArgs e)
{

Exception objErr = Server.GetLastError().GetBaseException();
string err ="Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +"\nError Message:" +
objErr.Message.ToString()+ "\nStack Trace:" + objErr.StackTrace.ToString();

EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
Server.ClearError();
classes.email Email = new classes.email();
bool Status = Email.GenerateEmail("ERROR",null,err.ToString());
Response.Redirect("errorpage.aspx");

}
 
M

Marina

By the time this event fires, it is too late to do a redirect. There is no
request at this point.

If you want to have central error handling, but be able to redirect, then
create a base page class that handles the error event. Then have every other
page in your application inherit from this page.
 
G

Guest

Ok, excluding the redirect line of code. At the point Im saying its noteven
getting to this point. My understanding was that all application error with
the exception of http error, ie 404,500 ect get passed to the
Application_Error method in the global.asax if there is no Page_Error method
to handle an error at the page level. Its as though it never makes it to the
global.asax.

Strange that intllisense will allow Response.Redirect but that you cant use
it. I see ehat your saying to do, Im just trying to understand that
everything Ive read says errors get passed to this method, yet Im not getting
ANY results.

Thoughts?
--
JP
..NET Software Develper


Marina said:
By the time this event fires, it is too late to do a redirect. There is no
request at this point.

If you want to have central error handling, but be able to redirect, then
create a base page class that handles the error event. Then have every other
page in your application inherit from this page.
 
P

Patrice

What if you are using a bare bone sub such as adding 1 to a counter and
seeing on another page if its change ?
My first goal would be to see if this is triggered or if there is some kind
of error in trapping the error that prevents this.

Also try to use off in the web.config for a start. The normal process is
that you would go to the custom page after the global handler. This might be
the problem.

--
Patrice

JP said:
Ok, excluding the redirect line of code. At the point Im saying its noteven
getting to this point. My understanding was that all application error with
the exception of http error, ie 404,500 ect get passed to the
Application_Error method in the global.asax if there is no Page_Error method
to handle an error at the page level. Its as though it never makes it to the
global.asax.

Strange that intllisense will allow Response.Redirect but that you cant use
it. I see ehat your saying to do, Im just trying to understand that
everything Ive read says errors get passed to this method, yet Im not getting
ANY results.

Thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top