Global.asax issue
-
Hey all, Am trying to use the global.asax file to handle any unhandled exceptions. I want it to do 2 things, log the error to a text file from a class and redirect the user to an error page. I'm sure I've done it before, but can't find my old code. This is what I'm using now:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
string errStr = "Message: - " + ex.Message.ToString() + ", Source: - " + ex.Source.ToString();
ErrorLogger.WriteErrorToFile(errStr);Response.Redirect("~/Error.aspx"); }
The event fires, but when I call my class it ignores that and the redirect and shows the original error on the page. If I take out the class call it does redirect, but I want a log of what happened. (the class writes the error to a text file using a streamwriter). Anyone have an inkling as to why this isn't working? The global files being generated by VS2005 for .net 2.0 if that helps thanks in advance Mark
-
Hey all, Am trying to use the global.asax file to handle any unhandled exceptions. I want it to do 2 things, log the error to a text file from a class and redirect the user to an error page. I'm sure I've done it before, but can't find my old code. This is what I'm using now:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
string errStr = "Message: - " + ex.Message.ToString() + ", Source: - " + ex.Source.ToString();
ErrorLogger.WriteErrorToFile(errStr);Response.Redirect("~/Error.aspx"); }
The event fires, but when I call my class it ignores that and the redirect and shows the original error on the page. If I take out the class call it does redirect, but I want a log of what happened. (the class writes the error to a text file using a streamwriter). Anyone have an inkling as to why this isn't working? The global files being generated by VS2005 for .net 2.0 if that helps thanks in advance Mark
You could try to clear the error after you handle it in your ErrorLogger, like so:
void Application\_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError().GetBaseException(); string errStr = "Message: - " + ex.Message.ToString() + ", Source: - " + ex.Source.ToString(); ErrorLogger.WriteErrorToFile(errStr); // Clear last exception... Server.ClearError(); Response.Redirect("~/Error.aspx"); }
Fernando Mendes Senior .NET Developer, Architect
-
You could try to clear the error after you handle it in your ErrorLogger, like so:
void Application\_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError().GetBaseException(); string errStr = "Message: - " + ex.Message.ToString() + ", Source: - " + ex.Source.ToString(); ErrorLogger.WriteErrorToFile(errStr); // Clear last exception... Server.ClearError(); Response.Redirect("~/Error.aspx"); }
Fernando Mendes Senior .NET Developer, Architect
Thanks for the reply, it didn't help, but I did find the cause. My dev environment wasn't debugging properly, it couldn't find the error log file so was skipping the rest of the steps in the Application_Error. Thanks again