.Net Core-Azure Application Insights不显示异常 [英] .Net Core - Azure Application Insights not showing exceptions

查看:68
本文介绍了.Net Core-Azure Application Insights不显示异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net核心项目在哪里以及如何在全球范围内将Azure应用程序见解例外置于何处?我已经安装了应用程序见解,可以完全了解遥测,但是失败的请求中没有例外.

Where and how to put azure application insights exceptions on global level in .net core project? I am having app insights installed and I can follow telemetry in azure, but exceptions are missing for failed requests.

推荐答案

一种方法是创建自定义中间件,该中间件将捕获错误并将异常发送给AppInsights.

One approach would be to create custom middleware which would catch the error and send the exception to AppInsights.

using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Middleware
{
    public static class ApplicationBuilderExtensions
    {
        public static IApplicationBuilder UseHttpException(this IApplicationBuilder application)
        {
            return application.UseMiddleware<HttpExceptionMiddleware>();
        }
    }

    public class HttpExceptionMiddleware
    {
        private readonly RequestDelegate _next;

        public HttpExceptionMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next.Invoke(context);
            }
            catch (Exception ex)
            {
                var telemetryClient = new TelemetryClient();
                telemetryClient.TrackException(ex);

                //handle response codes and other operations here
            }
        }
    }
}

然后,在Startup的Configure方法中注册中间件:

Then, register the middleware in the Startup's Configure method:

app.UseHttpException();

app.UseHttpException();

这篇关于.Net Core-Azure Application Insights不显示异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆