Solved

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

Posted on 2011-09-14
5
404 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
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 250 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 250 total points
ID: 36541377
Can you try after commenting,

ctx.Server.ClearError();          
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…

713 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