/// <summary> /// 进行异常处理(由异常管理器调用) /// </summary> /// <param name="e">异常</param> /// <param name="level">异常等级(传递给日志记录器处理)</param> /// <returns>处理结果,将影响下面的处理器</returns> /// <remarks> /// 异常管理器将根据返回的结果进行下一步的处理,约定:<br /> /// 返回的结果中,ResultNo值: /// <list type="bullet"> /// <item><description> /// 小于0:表示处理异常,管理器将立即退出异常处理 /// </description></item> /// <item><description> /// 0:处理正常 /// </description></item> /// <item><description> /// 1:已处理,需要下一个异常处理器进一步处理,<br /> /// 此时ResultAttachObject为返回的异常(可能与传入的异常是不一致的) /// </description></item> /// <item><description> /// 2:已处理,需要重新轮询异常处理器进行处理<br /> /// 此时ResultAttachObject为返回的异常(可能与传入的异常是不一致的)<br /> /// 此时异常管理器将重新进行异常处理 /// </description></item> /// </list> /// </remarks> public override IAOPResult Handle(Exception e, int level) { var ex = e as HttpWebException; if (ex != null) { var app = ex.HttpAppInstance; var sourceException = ExceptionBase.FindSourceException(ex); //忽略某些状态码,此时不记录日志 var writeLog = true; var ihcs = this.IgnoreHttpCodes; if (!string.IsNullOrEmpty(ignoreHttpCodes)) { var he = sourceException as HttpException; if (he != null) { var hc = "," + he.GetHttpCode() + ","; if (ihcs.Contains(hc)) { writeLog = false; } } } if (writeLog) { this.LogService.WriteLog(this, level, this.GetFormattedString(sourceException, level, app)); } //是否启用自定义信息 if (!this.IsCustomEnabled(ex)) { goto EXIT; } app.Server.ClearError(); app.Response.Clear(); var fileExists = false; if (this.DefaultRedirectUrl != null && this.CheckRedirectFileExists) { var filePath = WebHelper.UrlCombine(app.Request.ApplicationPath, this.DefaultRedirectUrl, false); fileExists = File.Exists(app.Request.MapPath(filePath)); } if (!fileExists && this.CheckRedirectFileExists) { this.LogService.WriteLog(this.GetType(), LogLevel.WARN, "WARNING: The file defined in HttpWebExceptionHandler's defaultRedirectUrl do not exists, please check it!"); } if (!string.IsNullOrEmpty(this.DefaultRedirectUrl) && (fileExists || !this.CheckRedirectFileExists) && !WebHelper.IsUrlEquals(this.DefaultRedirectUrl, app.Request)) { app.Response.Redirect(string.Format(this.DefaultRedirectUrl, sourceException.GetHashCode(), 0, level, HttpUtility.UrlEncode(HttpUtility.HtmlEncode(sourceException.Message), app.Request.ContentEncoding), HttpUtility.UrlEncode(HttpUtility.HtmlEncode(app.Request.Url.PathAndQuery), app.Request.ContentEncoding)), true); } else { app.Response.Write(this.GetDisplayMessage(ex)); app.CompleteRequest(); } } EXIT: return(AOPResult.Success()); }
/// <summary> /// WEB应用程序异常捕捉 /// </summary> private static void WebOnError(object sender, EventArgs e) { var httpApp = (HttpApplication)sender; var ex = httpApp.Server.GetLastError(); var ex0 = ExceptionBase.FindSourceException(ex); if (ex0 is FileNotFoundException) { return; } string message = null; if (ex != null) { message = ex.Message; } ExceptionService.Publish(new HttpWebException(message, ex, httpApp)); }