HandlerErrorAttribute.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Lottomat.Application.Busines.SystemManage;
  2. using Lottomat.Application.Code;
  3. using Lottomat.Application.Entity.SystemManage;
  4. using Lottomat.Util;
  5. using Lottomat.Util.Attributes;
  6. using Lottomat.Util.Extension;
  7. using Lottomat.Util.Log;
  8. using Lottomat.Util.WebControl;
  9. using System;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. using Lottomat.Utils.Date;
  13. using Lottomat.Utils.Web;
  14. namespace Lottomat.Application.Admin
  15. {
  16. /// <summary>
  17. /// 版 本 1.0
  18. /// Copyright (c) 2016-2017
  19. /// 创建人:赵轶
  20. /// 日 期:2015.11.9 10:45
  21. /// 描 述:错误日志(Controller发生异常时会执行这里)
  22. /// </summary>
  23. public class HandlerErrorAttribute : HandleErrorAttribute
  24. {
  25. /// <summary>
  26. /// 控制器方法中出现异常,会调用该方法捕获异常
  27. /// </summary>
  28. /// <param name="context">提供使用</param>
  29. public override void OnException(ExceptionContext context)
  30. {
  31. WriteLog(context);
  32. base.OnException(context);
  33. context.ExceptionHandled = true;
  34. context.HttpContext.Response.StatusCode = 200;
  35. context.Result = new ContentResult
  36. {
  37. Content = new AjaxResult<string>
  38. {
  39. type = ResultType.Error,
  40. message = context.Exception.Message
  41. }.ToJson()
  42. };
  43. }
  44. /// <summary>
  45. /// 写入日志(log4net)
  46. /// </summary>
  47. /// <param name="context">提供使用</param>
  48. private void WriteLog(ExceptionContext context)
  49. {
  50. if (context == null)
  51. return;
  52. if (OperatorProvider.Provider.IsOverdue())
  53. return;
  54. //var log = LogFactory.GetLogger(context.Controller.ToString());
  55. LogHelper logHelper = new LogHelper(context.Controller.ToString());
  56. Exception Error = context.Exception;
  57. LogMessage logMessage = new LogMessage
  58. {
  59. OperationTime = DateTimeHelper.Now,
  60. Url = HttpContext.Current.Request.RawUrl,
  61. Class = context.Controller.ToString(),
  62. Ip = NetHelper.Ip,
  63. Host = NetHelper.Host,
  64. Browser = NetHelper.Browser,
  65. UserName = OperatorProvider.Provider.Current().Account + "(" + OperatorProvider.Provider.Current().UserName + ")"
  66. };
  67. logMessage.ExceptionInfo = Error.InnerException == null ? Error.Message : Error.InnerException.Message;
  68. //logMessage.ExceptionSource = Error.Source;
  69. //logMessage.ExceptionRemark = Error.StackTrace;
  70. string strMessage = new LogFormat().ExceptionFormat(logMessage);
  71. logHelper.Error(strMessage);
  72. LogEntity logEntity = new LogEntity
  73. {
  74. CategoryId = (int)CategoryType.Exception,
  75. OperateTypeId = ((int) OperationType.Exception).ToString(),
  76. OperateType = OperationType.Exception.GetEnumDescription(),
  77. OperateAccount = logMessage.UserName,
  78. OperateUserId = OperatorProvider.Provider.Current().UserId,
  79. ExecuteResult = -1,
  80. ExecuteResultJson = strMessage
  81. };
  82. logEntity.WriteLog();
  83. SendMail(strMessage);
  84. }
  85. /// <summary>
  86. /// 发送邮件
  87. /// </summary>
  88. private void SendMail(string body)
  89. {
  90. bool errorToMail = ConfigHelper.GetValue("ErrorToMail").ToBool();
  91. if (errorToMail == true)
  92. {
  93. string systemName = ConfigHelper.GetValue("SystemName");//系统名称
  94. string address = ConfigHelper.GetValue("ErrorReportTo");
  95. MailHelper.Send(address, systemName + " - 发生异常", body.Replace("-", ""));
  96. }
  97. }
  98. }
  99. }