/// <summary> /// Обработка пользовательских исключений /// </summary> /// <param name="exception"></param> public void HandleCustomException(CustomException exception) { using (var output = GetOutput()) { output.WriteLine($"{DateTime.Now}: Пользовательское исключение {exception.ToString()}"); } }
/// <summary> /// Abre un archivo de texto y la añada la información de la CustomException /// </summary> /// <param name="e"></param> public void saveReport(CustomException e) { try { serviceArchivoTexto.Save(filePath, e.ToString()); } catch (FileNotFoundException ex) { } }
void Application_Error(object sender, EventArgs e) { if (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException) { HttpContext.Current.Response.Write("请输入合法的字符串【<a href=\"javascript:history.back(0);\">返回</a>】"); HttpContext.Current.Server.ClearError(); return; } if (HttpContext.Current.Server.GetLastError() is CustomException) { CustomException ex = HttpContext.Current.Server.GetLastError() as CustomException; HttpContext.Current.Response.ContentType = "text/plain"; HttpContext.Current.Response.Write(ex.ToString()); HttpContext.Current.Server.ClearError(); return; } //Server.GetLastError().GetBaseException(); string url = ""; try { url = HttpContext.Current.Request.Url.ToString(); } catch { } Exception excep = HttpContext.Current.Server.GetLastError(); if (excep.Message.StartsWith("The controller for path")) { //FYJ.Common.LogHelper.WriteLog("HTTP 404:" + url); } else if (excep.Message.StartsWith("__RequestVerificationToken")) { //FYJ.Common.LogHelper.WriteLog("跨站点攻击:" + url); HttpContext.Current.Response.Write(new MessageModel(-99, "服务器拒绝,原因:跨站点攻击").ToString()); HttpContext.Current.Server.ClearError(); } else { LogHelper.WriteLog(excep, Environment.NewLine + "系统异常:" + url); } if (!IsShowError(HttpContext.Current)) { HttpContext.Current.Response.Write("系统运行发生异常"); HttpContext.Current.Server.ClearError(); } }
static void TestFinally(CustomException n) { try { Console.WriteLine(n.ToString()); } catch (System.Exception ex) { System.Console.WriteLine(ex.ToString()); } finally { Console.WriteLine("Run code from finally block"); } }
void Application_Error(object sender, EventArgs e) { if (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException) { HttpContext.Current.Response.Write("请输入合法的字符串【<a href=\"javascript:history.back(0);\">返回</a>】"); HttpContext.Current.Server.ClearError(); return; } if (HttpContext.Current.Server.GetLastError() is CustomException) { CustomException ex = HttpContext.Current.Server.GetLastError() as CustomException; HttpContext.Current.Response.ContentType = "text/plain"; HttpContext.Current.Response.Write(ex.ToString()); HttpContext.Current.Server.ClearError(); return; } if (HttpContext.Current.Server.GetLastError() is BlogNotFindException) { //HttpContext.Current.Response.Write("根据Uri没有获取到博客,请到后台设置正确的域名"); string content = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/blognotfind.html")); HttpContext.Current.Response.ContentType = "text/html"; content = content.Replace("你访问的地址无效", "BlogNotFindException:你访问的地址无效"); HttpContext.Current.Response.Write(content); HttpContext.Current.Response.End(); //一定要加这句 否则可能输出缓存 输出2次内容 HttpContext.Current.Server.ClearError(); return; } //Server.GetLastError().GetBaseException(); string url = ""; try { url = HttpContext.Current.Request.Url.ToString(); } catch { } Exception excep = HttpContext.Current.Server.GetLastError(); if (excep.Message.StartsWith("The controller for path")) { //FYJ.Common.LogHelper.WriteLog("HTTP 404:" + url); } else if (excep.Message.Contains("__RequestVerificationToken")) { //FYJ.Common.LogHelper.WriteLog("跨站点攻击:" + url); HttpContext.Current.Response.Write(new MessageModel(-99, "服务器拒绝,原因:跨站点攻击").ToString()); HttpContext.Current.Server.ClearError(); } else { LogHelper.WriteLog(excep, Environment.NewLine + "系统异常:" + url); } if (!IsShowError(HttpContext.Current)) { HttpContext.Current.Response.Write("系统运行发生异常"); HttpContext.Current.Server.ClearError(); } }