我初始化登录到program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>();
后来,我在Startup.cs中进行了一些全局异常处理:
public static void ConfigureExceptionHandler(this IApplicationBuilder app)
{
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = 200;
});
});
}
我注意到,我的ITelemetryProcessor中的代码先于app.UseExceptionHandler中的代码执行。
因此,已将处理过的异常记录到Application Insights中。我该如何预防?
答案 0 :(得分:0)
好的,所以我找到了一个不错的解决方法。我使用自定义中间件来更改响应代码,因此只要在中间件中进行处理,该异常就不会弹出见解。示例:
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception ex)
{
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
}
}
}