| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using CallCenter.Utility;
- using CallCenterApi.Common;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Web.Mvc;
- namespace CallCenterApi.Interface
- {
- public class ErrorAttribute : HandleErrorAttribute
- {
- //此处代码说明,将所有网站异常的日志全部记录 log4net error文件
- public override void OnException(ExceptionContext context)
- {
- base.OnException(context);
- WriteLog(context);
- context.ExceptionHandled = true;
- context.HttpContext.Response.StatusCode = 200;
- //context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = context.Exception.ToString()}.ToJson() };
- context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = "系统异常,请稍后重试" }.ToJson() };
- }
- private void WriteLog(ExceptionContext context)
- {
- if (context == null)
- return;
- var actionName = context.RouteData.Values["Action"];
- var log = LogFactory.GetLogger(context.Controller.ToString() + "/" + actionName);
- var cnt = context.RequestContext.HttpContext;
- var rt = cnt.Request;
- string json = CommonHelper.RequestLog(rt);
- string usercode = cnt.User?.Identity.Name ?? "";
- log.Error("系统异常,操作账号:" + usercode + ",请求信息:" + json, context.Exception);
- }
- }
- }
|