Azure Monitor / Application Insights不显示错误的堆栈跟踪 [英] Azure Monitor / Application Insights not showing stack trace for errors

查看:163
本文介绍了Azure Monitor / Application Insights不显示错误的堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure App Service上托管了一个ASP .Net Core 3.0 Web API。
我想弄清楚为什么在一种控制器操作方法中抛出500 Internal Server Error。
我已经设置了Application Insights,并且在Azure Portal的失败页面上可以看到有500个例外。但是,我看不到它们的堆栈跟踪。我需要做些什么来在Application Insights或Azure Monitor中打开堆栈跟踪报告。
P.S.即使我的API在.Net Core 2.2上,它也没有显示堆栈跟踪,因此它不是.Net Core 3.0。

I've got an ASP .Net Core 3.0 Web API hosted on Azure App Service. I'm am trying to figure out why it's throwing a 500 Internal Server Error in one of the controller action methods. I've got Application Insights set up, and I can see on the "Failures" page on Azure Portal that there are a number of 500 exceptions. However, I cannot see a stack trace for them. Is there something I need to do to turn on stack trace reporting in Application Insights or Azure Monitor. P.S. Even when my API was on .Net Core 2.2, it also wasn't showing stack traces, so it's not a .Net Core 3.0 thing.

下面是一些屏幕截图:

推荐答案

如果未看到堆栈跟踪,则必须确保代码将异常记录在其中一个此处描述的方式:

If you are not seeing the stack trace you have to make sure your code logs the exceptions in one of the ways described in here:

https://docs.microsoft.com/zh-CN/azure/azure-monitor/app/asp-net-exceptions#exceptions

在MVC中,您必须使用以下命令:

in MVC you have to use this:

public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
            {
                //If customError is Off, then AI HTTPModule will report the exception
                if (filterContext.HttpContext.IsCustomErrorEnabled)
                {   //or reuse instance (recommended!). see note above
                    var ai = new TelemetryClient();
                    ai.TrackException(filterContext.Exception);
                }
            }
            base.OnException(filterContext);
        }

在.net核心中,它是在configureservice级别完成的:

in .net core it is done at the configureservice level:

public void ConfigureServices(IServiceCollection services)
{
    Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions aiOptions
                = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
    // Disables adaptive sampling.
    aiOptions.EnableAdaptiveSampling = false;

    // Disables QuickPulse (Live Metrics stream).
    aiOptions.EnableQuickPulseMetricStream = false;
    services.AddApplicationInsightsTelemetry(aiOptions);
}

如此处所述:

https:// docs。 microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

这篇关于Azure Monitor / Application Insights不显示错误的堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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