You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.4 KiB

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Znyc.CloudCar.Model.ViewModels.ReportsCallBack;
namespace Znyc.CloudCar.Filter
{
/// <summary>
/// 异常错误过滤
/// </summary>
public class ExceptionFilter : IExceptionFilter, IAsyncExceptionFilter
{
private readonly ILogger _logger;
public ExceptionFilter(ILogger<ExceptionFilter> 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;
}
}
}