【问题标题】:Azure Monitor / Application Insights not showing stack trace for errorsAzure Monitor / Application Insights 未显示错误堆栈跟踪
【发布时间】:2019-10-14 11:59:10
【问题描述】:

我有一个托管在 Azure 应用服务上的 ASP .Net Core 3.0 Web API。 我试图弄清楚为什么它会在其中一种控制器操作方法中引发 500 Internal Server Error。 我已经设置了 Application Insights,我可以在 Azure 门户的“失败”页面上看到有 500 个异常。但是,我看不到它们的堆栈跟踪。我需要做些什么才能在 Application Insights 或 Azure Monitor 中打开堆栈跟踪报告。 附言即使我的 API 在 .Net Core 2.2 上,它也没有显示堆栈跟踪,所以它不是 .Net Core 3.0 的东西。

以下是一些截图:

【问题讨论】:

  • 你试过这个吗:docs.microsoft.com/en-us/azure/azure-monitor/app/…?你在抓捕和追踪吗?
  • 谢谢阿纳斯。我看到了,但在文档的更下方,我看到了这个docs.microsoft.com/en-us/azure/azure-monitor/app/…,它指出“从 Application Insights Web SDK 版本 2.6(beta3 及更高版本)开始,Application Insights 会自动收集控制器方法中抛出的未处理异常,用于 WebAPI 2+” .现在我认为整个文档指的是 .Net Framework 而不是 .Net Core...
  • 有同样的问题。 @FabricioRodriguez - 你有没有想过如何获取堆栈跟踪/异常详细信息?

标签: azure asp.net-core-mvc azure-application-insights azure-monitoring


【解决方案1】:

在应用洞察查询窗口中,编写查询以显示异常并投影名为“详细信息”的属性。其中包含堆栈跟踪信息。

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

【讨论】:

  • 这是有史以来最棒的查询。谢谢@ΕГИІИО
  • 很棒的查询 - 仅供参考 - 您可以使用 coalesce 返回第一个带有值的变量并清理 iff 语句。此外,其中一些异常消息属性也可能已更改。
【解决方案2】:

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

https://docs.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);
        }

在 .net 核心中,它是在 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);
}

如此处所述:

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

【讨论】:

  • 谢谢阿纳斯。我回复了您对我的原始帖子的评论:“我看到了,但在文档的更下方,我看到了 docs.microsoft.com/en-us/azure/azure-monitor/app/…,其中指出“从 Application Insights Web SDK 版本 2.6(beta3 及更高版本)开始,Application Insights 收集未处理的异常为 WebAPI 2+ 自动抛出控制器方法“。现在我认为整个文档都指的是 .Net Framework 而不是 .Net Core”
  • 我想我正在使用 Application Insights 版本 9.1.913.1,作为记录。
  • 啊啊,不,我没看到。谢谢!这可能是答案。现在正在经历。很快就会通知您...
  • 可能是 nuget 问题,尝试使用 Visual Studio 调试并手动创建异常以先查看
【解决方案3】:

我发现在 Application Insights 的“失败”页面的“操作”选项卡中查看“失败的请求”不会显示堆栈跟踪。我想这是因为这部分与 HTTP 请求有关,仅此而已。但如果我改为切换到 Exceptions 选项卡,我可以在那里看到堆栈跟踪。我想这是与代码执行相关的部分,这就是它有堆栈跟踪的原因。

【讨论】:

  • 看起来相关性不起作用。如果它有效,那么异常将显示在“前 3 个异常”网格中的“操作”选项卡上,在端到端事务详细信息体验中(作为单独的行和请求的一部分)。
  • 您能否分享更多关于您如何检测应用程序的详细信息?
  • 如果关联有效,您会看到与此类似的图片:docs.microsoft.com/en-us/azure/azure-monitor/app/…
猜你喜欢
  • 2020-11-06
  • 2011-08-25
  • 2011-08-12
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
相关资源
最近更新 更多