FalconSSO/FAuth/Extensions/ApiExceptionFilterAttribute.cs

33 lines
1.1 KiB
C#
Raw Normal View History

using System;
2020-04-13 08:35:54 +08:00
using FAuth.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
2020-04-13 08:35:54 +08:00
namespace FAuth.Extensions
{
/// <summary>
/// Api控制器返回异常
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple = true,Inherited = true)]
public class ApiExceptionFilterAttribute:ExceptionFilterAttribute
{
public ILogger Logger { get; set; }
public ApiExceptionFilterAttribute(ILogger<ApiExceptionFilterAttribute> logger) {
this.Logger = logger;
}
public override void OnException(ExceptionContext context) {
2020-04-12 16:06:23 +08:00
var id = Guid.NewGuid().ToString("N");
this.Logger.LogError(id + "|" + context.Exception.ToString());
var result = new ApiErrorResult {
Message = context.Exception.Message,
2020-04-12 16:06:23 +08:00
Id = id,
};
context.Result = new JsonResult(result) { StatusCode = StatusCodes.Status500InternalServerError };
}
}
}