.NET CORE全局異常處理(自定義過濾器 ExceptionFilterAttribute、自定義中間件)

1、自定義中間件處理異常(推薦) 參考:https://www.csframework.com/archive/1/arc-1-20211230-4180.htm

using System.Net;
using System.Text.Json;
using ExceptionHandling.Models.Responses;

namespace ExceptionHandling.Middlewares;

public class ExceptionHandlingMiddleware
{
    private readonly RequestDelegate _next;  // 用來處理上下文請求  
    private readonly ILogger<ExceptionHandlingMiddleware> _logger;
    public ExceptionHandlingMiddleware(RequestDelegate next,ILogger<ExceptionHandlingMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task InvokeAsync(HttpContext httpContext)
    {
        try
        {
            await _next(httpContext); //要麼在中間件中處理,要麼被傳遞到下一個中間件中去
        }
        catch (Exception ex)
        {
            await HandleExceptionAsync(httpContext, ex); // 捕獲異常了在HandleExceptionAsync中處理
        }
    }
    private async Task HandleExceptionAsync(HttpContext context, Exception exception)
    {
        context.Response.ContentType = "application/json";  // 返回json 類型
        var response = context.Response;

        var errorResponse = new ErrorResponse
        {
            Success = false
        };  // 自定義的異常錯誤信息類型
        switch (exception)
        {
            case ApplicationException ex:
                if (ex.Message.Contains("Invalid token"))
                {
                    response.StatusCode = (int) HttpStatusCode.Forbidden;
                    errorResponse.Message = ex.Message;
                    break;
                }
                response.StatusCode = (int) HttpStatusCode.BadRequest;
                errorResponse.Message = ex.Message;
                break;
            case KeyNotFoundException ex:
                response.StatusCode = (int) HttpStatusCode.NotFound;
                errorResponse.Message = ex.Message;
                break;
            default:
                response.StatusCode = (int) HttpStatusCode.InternalServerError;
                errorResponse.Message = "Internal Server errors. Check Logs!";
                break;
        }
        _logger.LogError(exception.Message);
        var result = JsonSerializer.Serialize(errorResponse);
        await context.Response.WriteAsync(result);
    }
} 
注入中間件:app.UseMiddleware<ExceptionHandlingMiddleware>();然後在Action或者Service中直接拋異常,就會走異常處理。
2、使用過濾器 參考:https://blog.csdn.net/Daniel_yka/article/details/121062319
public class CustomExceptionFilterAttribute: ExceptionFilterAttribute
{
  private readonly ILogger<WeatherForecastController> _logger;
  public CustomExceptionFilterAttribute(ILogger<WeatherForecastController> logger)
  {
    _logger = logger;
  }
  public override void OnException(ExceptionContext context)
  {
    //判斷該異常有沒有處理
    if (!context.ExceptionHandled)
    {
      _logger.LogError($"Path:{context.HttpContext.Request.Path}Message:{context.Exception.Message}");
      context.Result = new JsonResult(new {Reslut = false,Msg = "發生異常,請聯繫管理員"});
      context.ExceptionHandled = true;
    }
  }
}

然後再在Startup下面的ConfigureServices註冊這個類:services.AddControllers(o=>o.Filters.Add(typeof(CustomExceptionFilterAttribute)));
此時當程序中有異常,便會進入該方法,我們就能在這裏統一管理異常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章