?
Solved

Redirecting errors to 500.aspx page in asp.net2.0

Posted on 2011-09-14
5
Medium Priority
?
407 Views
Last Modified: 2012-05-12
Hi,

I am working on asp.net2.0 application using C#  

I wanted to redirect all my ineternal server error pages to 500.aspx page and there it should print log with all details

How should I do it?

Please guide

Thanks
0
Comment
Question by:tia_kamakshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36540869
Use <customErrors> web-config element for redirection in case of specific or any error.

For logging you can use Application_Error event of HttpApplication class in Global.asax.

See this link
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36540874
0
 
LVL 3

Accepted Solution

by:
chandrasekar1 earned 1000 total points
ID: 36541213
Hi Tia,

above experts are right !!

You need to handle the internal server errors through page or application level, the better way you can go with Application level event in Global.asax

protected void Application_Error(Object sender, EventArgs e)
{
  // At this point we have information about the error
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo =
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source +
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

//Create a separate method “Exceptionlogging” which logs the     //exception through enterprise library or write the error info in a XML

  Exceptionlogging(errorInfo);
 
  // --------------------------------------------------
  // To let the page finish running we clear the error
  // --------------------------------------------------
  ctx.Server.ClearError ();
}

Then to redirect the error page 500.aspx, you need to set Custom error in web.config, Even you can set the more specific error redirection, for 404 error it will go to Pagenotfound.aspx

<customErrors mode="On" defaultRedirect="~/errors/500.aspx">
     <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
</customErrors>

0
 

Author Comment

by:tia_kamakshi
ID: 36541269
Hi,

In web.config I have written

<customErrors mode="On" defaultRedirect="~/errors/500.aspx">
                  <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
                  <error statusCode="500" redirect="~/errors/500.aspx" />
            </customErrors>
            
            
            <customErrors mode="On" defaultRedirect="~/errors/500.aspx">
                              <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
                              <error statusCode="500" redirect="~/errors/500.aspx" />
            </customErrors>
            
            
and in global.asax file i have written below code:

void Application_Error(object sender, EventArgs e)
    {        
       
        HttpContext ctx = HttpContext.Current;

        Exception exception = ctx.Server.GetLastError();
        Logger.Error(String.Format("{0}: errorInfo:{1}", ctx.Request.Url.ToString(), exception));
        ctx.Server.ClearError();          
       
    }
   
   
    But it is not forwarding the 500 errors to 500.aspx file
   

Please guide

Many Thanks
0
 
LVL 8

Assisted Solution

by:jagrut_patel
jagrut_patel earned 1000 total points
ID: 36541377
Can you try after commenting,

ctx.Server.ClearError();          
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question