简体   繁体   中英

Ajax errors logged twice by ELMAH

I am using ELMAH for error logging and if the error occurs during partial postback (from an event handler of a control wrapped in an update panel), then it is logging the errors twice. I am NOT using the "MsAjaxDeltaErrorLog" module, but instead programmatically logging the errors from "AsyncPostBackError" event handler of ScriptManager control. I am doing this to avoid redirection to generic page for errors occurred during partial postbacks. I turned off redirection in web.config. here is the code that logs the exception

    protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
    {
        HttpContext.Current.Items["AjaxError"] = "Ajax";
        ErrorSignal.FromCurrentContext().Raise(e.Exception,HttpContext.Current);
    }

    and in Global.asax, I am redirecting manually

protected void ErrorLog_Logged(object sender, ErrorLoggedEventArgs e)
{
  if (!HttpContext.Current.Items.Contains("AjaxError"))
  {
                    var pageName = e.Entry.Error.StatusCode == 404 ? "FileNotFoundPage" : "GenericErrorPage";
                    var redirectPage = ConfigurationManager.AppSettings[pageName];
                    Response.Redirect(String.Format("{0}?ExceptionId={1}&ExceptionMessage={2}", redirectPage, Server.UrlEncode(e.Entry.Id),
                                      Server.UrlEncode(e.Entry.Error.Message)));
  }
}

From Elmah website:
(http://code.google.com/p/elmah/wiki/MSAjax)

Potential Issues

Unfortunately, the MsAjaxDeltaErrorLogModule is not a perfect solution. Due to the way it works, there is an extremely small possibility that errors will get logged twice within ELMAH. For this to occur, the following must happen:

  1. The unhandled exception must occur during a Partial PostBack
  2. It must occur before the OnInit method of System.Web.UI.PageRequestManager is called.

This will mean that both the standard ELMAH modules and the MsAjaxDeltaErrorModule will capture the exception.

In reality, the chances of this happening are going to be fairly slender. .NET Framework 3.5

An updated version of Microsoft Ajax was shipped with version 3.5 of the .NET Framework. This new version changed the way that unexpected errors in Partial PostBacks were handled, removing the call to Response.End. Therefore, if you continue to use the MsAjaxDeltaErrorLogModule with this version of the framework, you will see all unhandled errors in Partial PostBacks being logged twice in ELMAH.*

Im on the same position you are, and everytime I get a asyn error I get the problem logged twiced, but I guess were stuck unless we modify ELMAH's source code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM