应用程序见解 - 配置堆栈快照

时间:2018-03-15 23:07:33

标签: c# azure exception azure-application-insights

我想在应用程序洞察中收集异常堆栈,以便稍后在本地PC上进行调试。

我有一个带有.NET 4.7.2框架的ASP.NET MVC 5.2应用程序。

我将NuGet包Microsoft.ApplicationInsights.SnapshotCollector添加到我的项目,我的应用洞察键:

<Add Type="Microsoft.ApplicationInsights.SnapshotCollector.SnapshotCollectorTelemetryProcessor, Microsoft.ApplicationInsights.SnapshotCollector">
  <!-- The default is true, but you can disable Snapshot Debugging by setting it to false -->
  <IsEnabled>true</IsEnabled>
  <!-- Snapshot Debugging is usually disabled in developer mode, but you can enable it by setting this to true. -->
  <!-- DeveloperMode is a property on the active TelemetryChannel. -->
  <IsEnabledInDeveloperMode>false</IsEnabledInDeveloperMode>
  <!-- How many times we need to see an exception before we ask for snapshots. -->
  <ThresholdForSnapshotting>1</ThresholdForSnapshotting>
  <!-- The maximum number of snapshots we collect for a single problem. -->
  <MaximumSnapshotsRequired>3</MaximumSnapshotsRequired>
  <!-- The maximum number of problems that we can be tracking at any time. -->
  <MaximumCollectionPlanSize>50</MaximumCollectionPlanSize>
  <!-- How often to reset problem counters. -->
  <ProblemCounterResetInterval>24:00:00</ProblemCounterResetInterval>
  <!-- The maximum number of snapshots allowed per day. -->
  <SnapshotsPerDayLimit>30</SnapshotsPerDayLimit>
  <!--Whether or not to collect snapshot in low IO priority thread.-->
  <SnapshotInLowPriorityThread>true</SnapshotInLowPriorityThread>
</Add>

(我按照文档:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-snapshot-debugger

我添加了这个控制器方法来测试:

    public ActionResult TestCatchException()
    {
        try
        {
            throw new WebException("Test web ex");
        }
        catch(WebException web_ex)
        {
            try
            {
                throw new Exception("Web Catch First Level", web_ex);
            }
            catch(Exception ex)
            {
                var telemetry = new TelemetryClient();
                telemetry.TrackException(ex);
            }
        }

        return RedirectToAction("Error", new { Text = "err" });
    }

然后我在azure portal上查看:

enter image description here

但无法下载异常快照。有什么问题?

1 个答案:

答案 0 :(得分:0)

感谢你提出非常详细的问题。根据您提供的信息,有三种可能的原因导致快照未上传。

首先,如果您在Visual Studio中调试应用程序,那么DeveloperMode将为true。请在Applicationinsights.config文件中将IsEnabledInDeveloperMode属性设置为true。

其次,异常是在我们捕获快照之前触发两次。我们第一次看到异常,我们会检查它是否被App Insights跟踪,如果是,我们会在第二次看到异常时收集快照。如果您从本地计算机运行应用程序,则需要在一次执行Web应用程序时触发两次异常。如果您在Azure中运行应用程序,则还需要触发异常两次,以确保应用程序之间不会重新启动。

第三,如果您在本地计算机上运行此操作,则可能在上传者完成上传快照之前关闭Web应用程序。生成异常后,让您的应用程序运行几分钟,以便让上传者有时间完成上传快照。此故障排除页面介绍了如何从上传器查看遥测,以获取有关正在发生的事情的更多详细信息:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-troubleshoot-snapshot-debugger