0

如何获得更详细的异常进行调试?有什么我必须启用的吗?SendAllExceptionsToClients

4

1 回答 1

1

使用自定义 ExceptionFilter,SendAllExceptionsToClients 能够更改此值

公共类 demoHttpApiHostModule : AbpModule { private const string DefaultCorsPolicyName = "Default";

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //if (hostingEnvironment.IsDevelopment())
        {
            context.Services.AddMvc(m => m.Filters.Add<CustomExceptionFilter>(1));
        }
    }

  public class CustomExceptionFilter : AbpExceptionFilter
{
    private readonly IExceptionToErrorInfoConverter _errorInfoConverter;
    private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder;
    private readonly IJsonSerializer _jsonSerializer;
    public ILogger<CustomExceptionFilter> Logger { get; set; }

    public CustomExceptionFilter(IExceptionToErrorInfoConverter errorInfoConverter,
        IHttpExceptionStatusCodeFinder statusCodeFinder,
        IJsonSerializer jsonSerializer) : base(errorInfoConverter, statusCodeFinder, jsonSerializer)
    {
        _errorInfoConverter = errorInfoConverter;
        _statusCodeFinder = statusCodeFinder;
        _jsonSerializer = jsonSerializer;

        Logger = NullLogger<CustomExceptionFilter>.Instance;

        ((DefaultExceptionToErrorInfoConverter)errorInfoConverter).SendAllExceptionsToClients = true;
    }

    protected override void HandleAndWrapException(ExceptionContext context)
    {
        //TODO: Trigger an AbpExceptionHandled event or something like that.

        context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true");
        context.HttpContext.Response.StatusCode = (int)_statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception);

        var remoteServiceErrorInfo = _errorInfoConverter.Convert(context.Exception);

        context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo));

        var logLevel = context.Exception.GetLogLevel();

        Logger.LogWithLevel(logLevel, $"---------- {nameof(RemoteServiceErrorInfo)} ----------");
        Logger.LogWithLevel(logLevel, _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true));
        Logger.LogException(context.Exception, logLevel);

        context.Exception = null; //Handled!
    }
}
于 2020-03-25T05:10:41.703 回答