Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

115 строки
4.4 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Loger;
  3. using Learun.Util;
  4. using Learun.Util.Operat;
  5. using System;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace Learun.Application.Web
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:陈彬彬
  14. /// 日 期:2017.03.08
  15. /// 描 述:错误日志(Controller发生异常时会执行这里)
  16. /// </summary>
  17. public class HandlerErrorAttribute : HandleErrorAttribute
  18. {
  19. /// <summary>
  20. /// 控制器方法中出现异常,会调用该方法捕获异常
  21. /// </summary>
  22. /// <param name="context">提供使用</param>
  23. public override void OnException(ExceptionContext context)
  24. {
  25. try
  26. {
  27. WriteLog(context);
  28. }
  29. catch (Exception)
  30. {
  31. }
  32. base.OnException(context);
  33. context.ExceptionHandled = true;
  34. context.HttpContext.Response.StatusCode = 200;
  35. string msg = "智慧校园提醒您:" + context.Exception.Message;
  36. if (msg == "智慧校园提醒您:所需的防伪表单字段“__RequestVerificationToken”不存在。")
  37. {
  38. msg = "系统貌似出问题了,可联系智慧校园官方智慧校园售后人员。";
  39. }
  40. context.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.exception, info = msg }.ToJson() };
  41. }
  42. /// <summary>
  43. /// 写入日志(log4net)
  44. /// </summary>
  45. /// <param name="context">提供使用</param>
  46. private void WriteLog(ExceptionContext context)
  47. {
  48. if (context == null)
  49. return;
  50. var userInfo = LoginUserInfo.Get();
  51. var log = LogFactory.GetLogger(context.Controller.ToString());
  52. Exception Error = context.Exception;
  53. LogMessage logMessage = new LogMessage();
  54. logMessage.OperationTime = DateTime.Now;
  55. logMessage.Url = HttpContext.Current.Request.RawUrl;
  56. logMessage.Class = context.Controller.ToString();
  57. logMessage.Ip = Net.Ip;
  58. logMessage.Host = Net.Host;
  59. logMessage.Browser = Net.Browser;
  60. if (userInfo != null)
  61. {
  62. logMessage.UserName = userInfo.account + "(" + userInfo.realName + ")";
  63. }
  64. if (Error.InnerException == null)
  65. {
  66. logMessage.ExceptionInfo = Error.Message;
  67. logMessage.ExceptionSource = Error.Source;
  68. logMessage.ExceptionRemark = Error.StackTrace;
  69. }
  70. else
  71. {
  72. logMessage.ExceptionInfo = Error.InnerException.Message;
  73. logMessage.ExceptionSource = Error.InnerException.Source;
  74. logMessage.ExceptionRemark = Error.InnerException.StackTrace;
  75. }
  76. string strMessage = new LogFormat().ExceptionFormat(logMessage);
  77. log.Error(strMessage);
  78. LogEntity logEntity = new LogEntity();
  79. logEntity.F_CategoryId = 4;
  80. logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString();
  81. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception);
  82. logEntity.F_OperateAccount = logMessage.UserName;
  83. if (userInfo != null)
  84. {
  85. logEntity.F_OperateUserId = userInfo.userId;
  86. }
  87. logEntity.F_ExecuteResult = -1;
  88. logEntity.F_ExecuteResultJson = strMessage;
  89. logEntity.F_Description = "PC端";
  90. logEntity.WriteLog();
  91. SendMail(strMessage);
  92. }
  93. /// <summary>
  94. /// 发送邮件
  95. /// </summary>
  96. /// <param name="body">消息</param>
  97. private void SendMail(string body)
  98. {
  99. bool ErrorToMail = Config.GetValue("ErrorToMail").ToBool();
  100. if (ErrorToMail == true)
  101. {
  102. string SystemName = Config.GetValue("SystemName");//系统名称
  103. string recMail = Config.GetValue("RereceiveErrorMail");//接收错误信息邮箱
  104. MailHelper.Send("receivebug@learun.cn", SystemName + " - 发生异常", body.Replace("-", ""));
  105. }
  106. }
  107. }
  108. }