Azure Monitor/Application Insights 不显示错误的堆栈跟踪

Fab*_*uez 9 azure azure-monitoring asp.net-core-mvc azure-application-insights

我有一个托管在 Azure 应用服务上的 ASP .Net Core 3.0 Web API。我试图找出为什么它在控制器操作方法之一中抛出 500 内部服务器错误。我已经设置了 Application Insights,并且可以在 Azure 门户上的“失败”页面上看到有多个 500 异常。但是,我看不到它们的堆栈跟踪。我需要做什么才能在 Application Insights 或 Azure Monitor 中打开堆栈跟踪报告。PS 即使我的 API 在 .Net Core 2.2 上,它也没有显示堆栈跟踪,因此它不是 .Net Core 3.0 的东西。

这是一些屏幕截图:

在此输入图像描述

在此输入图像描述

Ε Г*_*И О 10

在应用程序见解查询窗口中,编写一个查询来显示异常并投影名为“详细信息”的属性。其中包含堆栈跟踪信息。

 exceptions
| where timestamp > ago(30d)
| order by timestamp asc
| project timestamp, message = iff(message != '', message, iff(innermostMessage != '', innermostMessage, customDimensions.['prop__{OriginalFormat}'])), details
Run Code Online (Sandbox Code Playgroud)


Ana*_*tit 1

如果您没有看到堆栈跟踪,则必须确保您的代码以此处描述的方式之一记录异常:

https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#exceptions

在 MVC 中你必须使用这个:

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);
        }
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

如此处所述:

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