Find and diagnose run-time exceptions with Azure Application Insights
Azure Application Insights collects telemetry from your application to help identify and diagnose run-time exceptions. This tutorial takes you through this process with your application. You learn how to:
- Modify your project to enable exception tracking
- Identify exceptions for different components of your application
- View details of an exception
- Download a snapshot of the exception to Visual Studio for debugging
- Analyze details of failed requests using query language
- Create a new work item to correct the faulty code
To complete this tutorial:
- Install Visual Studio 2017 with the following workloads:
- ASP.NET and web development
- Azure development
- Download and install the Visual Studio Snapshot Debugger.
- Enable Visual Studio Snapshot Debugger
- Deploy a .NET application to Azure and enable the Application Insights SDK.
- The tutorial tracks the identification of an exception in your application, so modify your code in your development or test environment to generate an exception.
Log in to Azure
Log in to the Azure portal at https://portal.azure.com.
Application Insights collects any failures in your application and lets you view their frequency across different operations to help you focus your efforts on those with the highest impact. You can then drill down on details of these failures to identify root cause.
- Select Application Insights and then your subscription.
To open the Failures panel either select Failures under the Investigate menu or click the Failed requests graph.
The Failed requests panel shows the count of failed requests and the number of users affected for each operation for the application. By sorting this information by user you can identify those failures that most impact users. In this example, the GET Employees/Create and GET Customers/Details are likely candidates to investigate because of their large number of failures and impacted users. Selecting an operation shows further information about this operation in the right panel.
Reduce the time window to zoom in on the period where the failure rate shows a spike.
Click View Details to see the details for the operation. This includes a Gantt chart that shows two failed dependencies which collectively took almost half of a second to complete. You can find out more about analyzing performance issues by completing the tutorial Find and diagnose performance issues with Azure Application Insights.
The operations detail also shows a FormatException which appears to have caused the failure. Click the exception or on the Top 3 exception types count to view its details. You can see that it's due to an invalid zip code.
Enable the "Unified details: E2E Transaction Diagnostics" preview experience to see all related server-side telemetry like requests, dependencies, exceptions, traces, events etc. in a single full screen view.
With the preview enabled, you can see the time spent in dependency calls, along with any failures or exceptions in a unified experience. For cross-component transactions, the Gantt chart along with the details pane can help you quickly diagnose the root-cause component, dependency or exception. You can expand the bottom section to see time-sequence of any traces or events collected for the selected component-operation. Learn more about the new experience
Identify failing code
The Snapshot Debugger collects snapshots of the most frequent exceptions in your application to assist you in diagnosing its root cause in production. You can view debug snapshots in the portal to see the call stack and inspect variables at each call stack frame. You can then debug the source code by downloading the snapshot and opening it in Visual Studio 2017.
- In the properties of the exception, click Open debug snapshot.
The Debug Snapshot panel opens with the call stack for the request. Click any method to view the values of all local variables at the time of the request. Starting from the top method in this example, we can see local variables that have no value.
The first call that has valid values is ValidZipCode, and we can see that a zip code was provided with letters that isn't able to be translated into an integer. This appears to be the error in the code that needs to be corrected.
To download this snapshot into Visual Studio where we can locate the actual code that needs to be corrected, click Download Snapshot.
- The snapshot is loaded into Visual Studio.
You can now run a debug session in Visual Studio that quickly identifies the line of code that caused the exception.
Use analytics data
All data collected by Application Insights is stored in Azure Log Analytics, which provides a rich query language that allows you to analyze the data in a variety of ways. We can use this data to analyze the requests that generated the exception we're researching.
Click the CodeLens information above the code to view telemetry provided by Application Insights.
Click Analyze impact to open Application Insights Analytics. It's populated with several queries that provide details on failed requests such as impacted users, browsers, and regions.
Add work item
If you connect Application Insights to a tracking system such as Visual Studio Team Services or GitHub, you can create a work item directly from Application Insights.
- Return to the Exception Properties panel in Application Insights.
- Click New Work Item.
The New Work Item panel opens with details about the exception already populated. You can add any additional information before saving it.
Now that you've learned how to identify run-time exceptions, advance to the next tutorial to learn how to identify and diagnose performance issues.