Geen omschrijving

ErrorAttribute.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using CallCenter.Utility;
  2. using CallCenterApi.Common;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web.Mvc;
  7. namespace CallCenterApi.Interface
  8. {
  9. public class ErrorAttribute : HandleErrorAttribute
  10. {
  11. //此处代码说明,将所有网站异常的日志全部记录 log4net error文件
  12. public override void OnException(ExceptionContext context)
  13. {
  14. base.OnException(context);
  15. WriteLog(context);
  16. context.ExceptionHandled = true;
  17. context.HttpContext.Response.StatusCode = 200;
  18. //context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = context.Exception.ToString()}.ToJson() };
  19. context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = "系统异常,请稍后重试" }.ToJson() };
  20. }
  21. private void WriteLog(ExceptionContext context)
  22. {
  23. if (context == null)
  24. return;
  25. var actionName = context.RouteData.Values["Action"];
  26. var log = LogFactory.GetLogger(context.Controller.ToString() + "/" + actionName);
  27. var cnt = context.RequestContext.HttpContext;
  28. var rt = cnt.Request;
  29. string json = CommonHelper.RequestLog(rt);
  30. string usercode = cnt.User?.Identity.Name ?? "";
  31. log.Error("系统异常,操作账号:" + usercode + ",请求信息:" + json, context.Exception);
  32. }
  33. }
  34. }