ExceptionFilterFor.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.AspNetCore.Mvc.Filters;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Cksoft.Unity.Log4NetConfig;
  7. using NLog;
  8. namespace DllEapCommon.Filters
  9. {
  10. public class ExceptionFilterFor : ExceptionFilterAttribute
  11. {
  12. private Logger logger;
  13. public override void OnException(ExceptionContext context)
  14. {
  15. if (!(context.HttpContext.Request.Path.HasValue && context.HttpContext.Request.Path.Value.StartsWith("/adt")))
  16. {
  17. var controllerName = context.ActionDescriptor.RouteValues["controller"]; // 控制器名称
  18. var actionName = context.ActionDescriptor.RouteValues["action"]; // 方法名称
  19. string errorinfo = $@"/{controllerName}/{actionName}请求异常:{context.Exception.Message}/{context.Exception.StackTrace}";
  20. var userCode = context.HttpContext.Request.Headers["usercode"];
  21. this.logger = LogManager.GetLogger(controllerName);
  22. //var eventInfo = LogHelper<ExceptionFilterFor>.CreateLogEventInfo(LogLevel.Error,
  23. // errorinfo, "系统日志", userCode);
  24. LogHelper<ExceptionFilterFor>.LogError(context.Exception.Message, "系统日志", userCode);
  25. // logger.Log(eventInfo);
  26. var errorMsg = context.Exception.Message;
  27. context.Result = new ContentResult { Content = errorMsg, StatusCode = 500, ContentType = "application/json" };
  28. context.ExceptionHandled = true;
  29. }
  30. }
  31. }
  32. }