简体   繁体   English

一旦我添加了自定义 ExceptionFilter,Serilog 就会停止记录异常

[英]Serilog stops logging exceptions as soon as I add a custom ExceptionFilter

I've added Serilog to my project:我已将 Serilog 添加到我的项目中:

Program.cs程序.cs

.UseSerilog((provider, context, loggerConfiguration) =>
{
    loggerConfiguration
        .MinimumLevel.Verbose()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning);
        .WriteTo.MSSqlServer(output, tableName: "Log", schemaName: "WPS")
        .Enrich.WithAspnetcoreHttpcontext(provider)
        .Enrich.WithExceptionDetails();
});

I've also added a service for handling API errors returned to the UI:我还添加了一项服务,用于处理返回到 UI 的 API 错误:

Startup.cs启动.cs

// Add Error Handling to all API endpoints
services.AddMvc(options => {
      options.Filters.Add(new ApiExceptionFilter());
});

ApiExceptionFilter.cs ApiExceptionFilter.cs

public class ApiExceptionFilter : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        ApiError apiError = null;
        if (context.Exception is ApiException)
        {
            var ex = context.Exception as ApiException;
            context.Exception = null;
            apiError = new ApiError(ex.Message);
            apiError.Errors = ex.Errors;
            context.HttpContext.Response.StatusCode = ex.StatusCode;
        }
        else if (context.Exception is UnauthorizedAccessException)
        {
            apiError = new ApiError("Unauthorized Access");
            context.HttpContext.Response.StatusCode = 403;
        }
        else
        {
            string msg = context.Exception.GetBaseException().Message;
#if !DEBUG
            string stack = "";
#else
            string stack = context.Exception.StackTrace;
#endif

            apiError = new ApiError(msg);
            apiError.Detail = stack;
            context.HttpContext.Response.StatusCode = 500;
        }
        context.Result = new JsonResult(apiError);

        base.OnException(context);
    }
}

However, after adding the ApiExceptionFilter, exceptions are not logged anymore.但是,添加 ApiExceptionFilter 后,不再记录异常。

I have have tried throwing the exception again, but then my custom message is not returned to the UI anymore.我已经尝试再次抛出异常,但是我的自定义消息不再返回到 UI。

ApiExceptionFilter.cs ApiExceptionFilter.cs

        base.OnException(context);
        throw context.Exception.GetBaseException(); // throw again?

Any suggestions on where I'm going wrong?关于我要去哪里错的任何建议?

Your best bet, here, is to catch and filter out the exception as you're doing, here, but also to log it in your exception filter.在这里,您最好的选择是在您正在执行的操作时捕获并过滤掉异常,同时也将其记录在您的异常过滤器中。

Generally the code the catches and handles an exception is responsible for logging it;通常,捕获和处理异常的代码负责记录它; that's what's happening without the filter - top-level exception handler is catching and logging the exception.这就是没有过滤器的情况 - 顶级异常处理程序正在捕获并记录异常。 Once you start handling the exception yourself, you're also responsible for logging it.一旦您开始自己处理异常,您也有责任记录它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM