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.CreateLogEventInfo(LogLevel.Error, // errorinfo, "系统日志", userCode); LogHelper.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; } } } }