By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,659 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,659 IT Pros & Developers. It's quick & easy.

using Global asax for error trapping

P: n/a
JP
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,EventLogEn tryType.Error);
Server.ClearError();
classes.email Email = new classes.email();
bool Status = Email.GenerateEmail("ERROR",null,err.ToString());
Response.Redirect("errorpage.aspx");

}

--
JP
..NET Software Developer

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"JP" <JP@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
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,EventLogEn tryType.Error);
Server.ClearError();
classes.email Email = new classes.email();
bool Status = Email.GenerateEmail("ERROR",null,err.ToString());
Response.Redirect("errorpage.aspx");

}

--
JP
.NET Software Developer

Nov 22 '05 #2

P: n/a
JP
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" wrote:
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.

"JP" <JP@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
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,EventLogEn tryType.Error);
Server.ClearError();
classes.email Email = new classes.email();
bool Status = Email.GenerateEmail("ERROR",null,err.ToString());
Response.Redirect("errorpage.aspx");

}

--
JP
.NET Software Developer


Nov 22 '05 #3

P: n/a
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" <JP@discussions.microsoft.com> a crit dans le message de
news:9B**********************************@microsof t.com...
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" wrote:
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.

"JP" <JP@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
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,EventLogEn tryType.Error);
Server.ClearError();
classes.email Email = new classes.email();
bool Status = Email.GenerateEmail("ERROR",null,err.ToString());
Response.Redirect("errorpage.aspx");

}

--
JP
.NET Software Developer


Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.