MVC异常捕获处理,FilterConfig:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new MyErrorAttribute()); } } public class MyErrorAttribute : HandleErrorAttribute { public void LogException(ExceptionContext filterContext, ExceptionType exceptionType) { //var url = filterContext.RouteData.Values["controller"] + "/" + filterContext.RouteData.Values["action"]; string exception = string.Empty; string data = string.Empty; string url = string.Empty; string message = string.Empty; string controllerName = string.Empty; string actionName = string.Empty; int userId = 0; try { if (filterContext.Exception.InnerException != null) { exception = JsonConvert.SerializeObject(filterContext.Exception.InnerException, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } else { exception = JsonConvert.SerializeObject(filterContext.Exception, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } string name = System.Web.HttpContext.Current.User.Identity.Name; UserTicket userTicket = new UserTicket(); if (!string.IsNullOrEmpty(name)) { FormsIdentity id = (FormsIdentity)System.Web.HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; userTicket = JsonHelper.ParseJSON<UserTicket>(id.Ticket.UserData); #region 返回当前用户 userId = userTicket.UserId; #endregion } url = JsonConvert.SerializeObject(filterContext.RequestContext.HttpContext.Request.Url); message = filterContext.Exception.Message; userId = userId; //filterContext.RequestContext.HttpContext.User.Identity.Name.ToInt32(); controllerName = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString(); actionName = filterContext.Controller.ControllerContext.RouteData.Values["action"].ToString(); var d = System.Web.HttpContext.Current.Request.InputStream; d.Position = 0; var postData = new StreamReader(d).ReadToEnd(); var qdata = string.Empty; var fdata = string.Empty; for (int i = 0; i < filterContext.RequestContext.HttpContext.Request.Form.Count; i++) { fdata += filterContext.RequestContext.HttpContext.Request.Form.Keys[i].ToString() + "=" + filterContext.RequestContext.HttpContext.Request.Form[i].ToString() + "&"; } for (int i = 0; i < filterContext.RequestContext.HttpContext.Request.QueryString.Count; i++) { qdata += filterContext.RequestContext.HttpContext.Request.QueryString.Keys[i].ToString() + "=" + filterContext.RequestContext.HttpContext.Request.QueryString[i].ToString() + "&"; } var request = new { Content = new { Query = qdata, Form = fdata, Body = postData }, filterContext.RequestContext.HttpContext.Request.Headers, Method = filterContext.RequestContext.HttpContext.Request.HttpMethod, filterContext.RequestContext.HttpContext.Request.RawUrl, filterContext.RequestContext.HttpContext.Request.UserAgent }; data = JsonConvert.SerializeObject(request, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } catch (Exception ex) { message = ex.Message; exceptionType = Common.ExceptionType.LogException; } finally { ExceptionLog exceptionLog = new ExceptionLog(); ExceptionLogBLL exceptionLogBll=new ExceptionLogBLL(); exceptionLogBll.AddReturnId(new ExceptionLog() { CreateDate = DateTime.Now, Url=url, Type = exceptionType.ToString(), ExceptionStack=exception, RequestData = data, Message = message, UserId = userId, Status=BoolType.Fou, //0:未处理 1:已处理 OrderIndex=0, IsDelete = false, ControllerName = controllerName, ActionName = actionName, IsFront = false }); } } /// <summary> /// 在发生异常时调用。 /// </summary> /// <param name="filterContext">操作筛选器上下文。</param> public override void OnException(ExceptionContext filterContext) { //base.OnException(filterContext); if (filterContext.Exception.GetType() == typeof(JxException)) { LogException(filterContext, Common.ExceptionType.JxException); filterContext.HttpContext.Response.Redirect("~/ShowMsg/Index?msg=" + filterContext.Exception.Message); //filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "ShowMsg", action = "Index", msg = filterContext.Exception.Message })); } else if (filterContext.Exception.GetType() == typeof(JxSuccessMessage)) { filterContext.HttpContext.Response.Redirect("~/ShowMsg/Index?msg=" + filterContext.Exception.Message); } else if (filterContext.Exception.GetType() == typeof(JxSecurityException)) { LogException(filterContext, Common.ExceptionType.JxSecurityException); filterContext.HttpContext.Response.Redirect("~/ShowMsg/Index?msg=" + filterContext.Exception.Message); } else if (filterContext.Exception.GetType() == typeof(JxMessage)) { filterContext.HttpContext.Response.Redirect("~/ShowMsg/Index?msg=" + filterContext.Exception.Message); } else if (filterContext.Exception.GetType() == typeof(JxInsideException)) { LogException(filterContext, Common.ExceptionType.JxInsideException); filterContext.HttpContext.Response.Redirect("~/ShowMsg/Index?msg=" + filterContext.Exception.Message); } else { LogException(filterContext, Common.ExceptionType.UnknownException); base.OnException(filterContext); } } }
异常枚举类:
public class ExceptionEnum { public static Dictionary<string, string> Dictionary = new Dictionary<string, string>() { {ExceptionType.JxException.ToString(), "异常"}, {ExceptionType.JxSecurityException.ToString(), "安全性异常"}, {ExceptionType.JxInsideException.ToString(), "程序代码异常"}, {ExceptionType.UnknownException.ToString(), "隐性异常"}, {ExceptionType.LogException.ToString(), "异常记录异常"}, {ExceptionType.Other.ToString(), "其他"} }; } #region Exception public enum ExceptionType { /// <summary> /// 异常 /// </summary> [Description("异常")] JxException, /// <summary> /// 安全性异常 /// </summary> [Description("安全性异常")] JxSecurityException, /// <summary> /// 程序代码异常 /// </summary> [Description("程序代码异常")] JxInsideException, /// <summary> /// 隐性异常 /// </summary> [Description("隐性异常")] UnknownException, /// <summary> /// 异常记录异常 /// </summary> [Description("异常记录异常")] LogException, /// <summary> /// 其他 /// </summary> [Description("其他")] Other } #endregion Exception
原文地址:https://www.cnblogs.com/sharing1986687846/p/10362195.html
时间: 2024-10-29 21:01:36