using Learun.Application.Base.SystemModule;
using Learun.Loger;
using Learun.Util;
using Learun.Util.Operat;
using System;
using System.Web;
using System.Web.Mvc;
namespace Learun.Application.Web
{
///
/// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
/// Copyright (c) 2013-2018 Hexagon PPM
/// 创建人:-框架开发组
/// 日 期:2017.03.08
/// 描 述:错误日志(Controller发生异常时会执行这里)
///
public class HandlerErrorAttribute : HandleErrorAttribute
{
///
/// 控制器方法中出现异常,会调用该方法捕获异常
///
/// 提供使用
public override void OnException(ExceptionContext context)
{
try
{
WriteLog(context);
}
catch (Exception)
{
}
base.OnException(context);
context.ExceptionHandled = true;
context.HttpContext.Response.StatusCode = 200;
string msg = "温馨提醒:" + context.Exception.Message;
if (msg == "温馨提醒:所需的防伪表单字段“__RequestVerificationToken”不存在。")
{
msg = "系统貌似出问题了,可联系系统管理员。";
}
context.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.exception, info = msg }.ToJson() };
}
///
/// 写入日志(log4net)
///
/// 提供使用
private void WriteLog(ExceptionContext context)
{
if (context == null)
return;
var userInfo = LoginUserInfo.Get();
var log = LogFactory.GetLogger(context.Controller.ToString());
Exception Error = context.Exception;
LogMessage logMessage = new LogMessage();
logMessage.OperationTime = DateTime.Now;
logMessage.Url = HttpContext.Current.Request.RawUrl;
logMessage.Class = context.Controller.ToString();
logMessage.Ip = Net.Ip;
logMessage.Host = Net.Host;
logMessage.Browser = Net.Browser;
if (userInfo != null)
{
logMessage.UserName = userInfo.account + "(" + userInfo.realName + ")";
}
SetExMessage(logMessage, Error);
string strMessage = new LogFormat().ExceptionFormat(logMessage);
log.Error(strMessage);
LogEntity logEntity = new LogEntity();
logEntity.F_CategoryId = 4;
logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString();
logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception);
logEntity.F_OperateAccount = logMessage.UserName;
if (userInfo != null)
{
logEntity.F_OperateUserId = userInfo.userId;
}
logEntity.F_ExecuteResult = -1;
logEntity.F_ExecuteResultJson = strMessage;
logEntity.WriteLog();
SendMail(strMessage);
}
private void SetExMessage(LogMessage logMessage, Exception ex)
{
if (ex.InnerException != null && ex.InnerException.Message != null && ex.InnerException.Source != null && ex.InnerException.StackTrace != null )
{
SetExMessage(logMessage, ex.InnerException);
}
else
{
logMessage.ExceptionInfo = ex.Message;
logMessage.ExceptionSource = ex.Source;
logMessage.ExceptionRemark = ex.StackTrace;
}
}
///
/// 发送邮件
///
/// 消息
private void SendMail(string body)
{
bool ErrorToMail = Config.GetValue("ErrorToMail").ToBool();
if (ErrorToMail == true)
{
string SystemName = Config.GetValue("SystemName");//系统名称
string recMail = Config.GetValue("RereceiveErrorMail");//接收错误信息邮箱
MailHelper.Send("receivebug@learun.cn", SystemName + " - 发生异常", body.Replace("-", ""));
}
}
}
}