سؤال

I am attempting to catch all errors except '404-File not found' error in global.asax file and writing in to a text file in server,But lot of errors gets missed ,like FormatException Error,Sessionout Error and "Yellow Page of Error" Appears !

How can I catch all errors here.

void Application_Error(object sender, EventArgs e) 
{
    CT.Bussiness.DevCommonFunctions devTools = new CT.Bussiness.DevCommonFunctions();
    HttpContext ctx = HttpContext.Current;
    //ctx.Response.
    // Get page url
    if (ctx != null)
    {
        string pageName = ctx.Request.Url.ToString();

        Exception ex = Server.GetLastError().GetBaseException();
        HttpException checkException = (HttpException)ex;
        if (checkException.GetHttpCode() != 404)
        {
            string errorDate = DateTime.Now.ToString();
            //string pageName =pageName;
            string errorMessage = ex.Message;
            string errorSource = ex.Source;
            //string errorInnerException=ex.InnerException.ToString();
            string errorData = ex.Data.ToString();
            string errorTarget = ex.TargetSite.ToString();
            string errorStack = ex.StackTrace.ToString();
            devTools.WriteErrorsIntoErrorLog(errorDate, pageName, errorMessage, errorSource, "InnerException", errorData, errorTarget, errorStack);
            //devTools.SendErrorMail("developer3@devwebservices.net,developer4@devwebservices.net,developer5@devwebservices.net,hr@devwebservices.net", errorDate, pageName, errorMessage, errorSource, "InnerException", errorData, errorTarget, errorStack);
            //Response.TrySkipIisCustomErrors = true;
            Response.Redirect("~/Error.aspx");
            //Response.Redirect("Error.aspx");

        }
    }
}

SOLVED

I Followed Like this as KPL Adviced below:

  1. Implemented Elmah
  2. Excluded Global.asax and
  3. Wrote redirection code in web.config.

Thus Solved !

هل كانت مفيدة؟

المحلول

Use:

1.) web.config "customErrors" tag to avoid "Yellow Page of Error":

<customErrors mode="On" defaultRedirect="~/Error.aspx">
      <error statusCode="400" redirect="/error/400-bad-request/" />
      <error statusCode="404" redirect="/error/404-file-not-found/" />
      <error statusCode="500" redirect="/error/500-server-error/" />
</customErrors>

2.) Also try using Elmah to logging errors.

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:

  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recoded exceptions.
  • A web page to remotely view the full details of any one logged exception, including colored stack traces.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top