using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace zhengcaioa.Model { public class HttpGlobalExceptionFilter : ExceptionFilterAttribute { private readonly ILogger _logger; public HttpGlobalExceptionFilter(ILogger logger) { _logger = logger; } public override void OnException(ExceptionContext context) { var actionName = context.HttpContext.Request.RouteValues["controller"] + "/" + context.HttpContext.Request.RouteValues["action"]; _logger.LogError($"--------{actionName} Error Begin--------"); _logger.LogError($" Error Detail:" + context.Exception.Message); //拦截处理 if (!context.ExceptionHandled) { ContentResult Content = new ContentResult(); Content.Content = GetRestContent(context.Exception.Message); Content.ContentType = "text/html"; context.Result = Content; context.ExceptionHandled = true; } _logger.LogError($"--------{actionName} Error End--------"); } public static string GetRestContent(string msg) { StringBuilder jsContent = new StringBuilder(); jsContent.AppendLine(" "); return jsContent.ToString(); } } }