12345678910111213141516171819202122232425262728293031323334 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.AspNetCore.Mvc.Filters;
- using Microsoft.AspNetCore.Mvc;
- using Cksoft.Unity.Log4NetConfig;
- using NLog;
- namespace DllEapCommon.Filters
- {
- public class ExceptionFilterFor : ExceptionFilterAttribute
- {
- private Logger logger;
- public override void OnException(ExceptionContext context)
- {
- if (!(context.HttpContext.Request.Path.HasValue && context.HttpContext.Request.Path.Value.StartsWith("/adt")))
- {
- var controllerName = context.ActionDescriptor.RouteValues["controller"]; // 控制器名称
- var actionName = context.ActionDescriptor.RouteValues["action"]; // 方法名称
- string errorinfo = $@"/{controllerName}/{actionName}请求异常:{context.Exception.Message}/{context.Exception.StackTrace}";
- var userCode = context.HttpContext.Request.Headers["usercode"];
- this.logger = LogManager.GetLogger(controllerName);
- //var eventInfo = LogHelper<ExceptionFilterFor>.CreateLogEventInfo(LogLevel.Error,
- // errorinfo, "系统日志", userCode);
- LogHelper<ExceptionFilterFor>.LogError(context.Exception.Message, "系统日志", userCode);
- // logger.Log(eventInfo);
- var errorMsg = context.Exception.Message;
- context.Result = new ContentResult { Content = errorMsg, StatusCode = 500, ContentType = "application/json" };
- context.ExceptionHandled = true;
- }
- }
- }
- }
|