Solved

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

Posted on 2011-09-14
5
399 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
Comment Utility
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
Comment Utility
0
 
LVL 3

Accepted Solution

by:
chandrasekar1 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Can you try after commenting,

ctx.Server.ClearError();          
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

9 Experts available now in Live!

Get 1:1 Help Now