Solved

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

Posted on 2011-09-14
5
400 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now