using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Znyc.CloudCar.Model.ViewModels.ReportsCallBack; namespace Znyc.CloudCar.Filter { /// /// 异常错误过滤 /// public class ExceptionFilter : IExceptionFilter, IAsyncExceptionFilter { private readonly ILogger _logger; public ExceptionFilter(ILogger logger) { _logger = logger; } public void OnException(ExceptionContext context) { Console.WriteLine(context.Exception); Console.WriteLine(context.Exception.InnerException); _logger.LogError(context.Exception, context.Exception.Message); ResponseOutput response = new() { Successed = false, Msg = "系统内部错误" }; context.Result = new InternalServerErrorResult(response); } public Task OnExceptionAsync(ExceptionContext context) { OnException(context); return Task.CompletedTask; } } public class InternalServerErrorResult : ObjectResult { public InternalServerErrorResult(object value) : base(value) { StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError; } } }