Microsoft announced an exciting new feature of Application Insights that's gonna save developers a lot of time when solving production exceptions. This new feature was announced during Build 2017 and is called snapshots for Application Insights. Build 2017 is a Microsoft conference where always tons of new features for different products are announced. Microsoft is continuously improving their product Application Insights by adding new features on a regular basis. The previous big feature was the profiling capability which provides insights in hot paths in code when requests are executed. In this blog, I'll guide you through the new snapshots feature.

I earlier blogged about how you can use Application Insights for solving exceptions. It's sometimes a pain for developers to solve exceptions especially when there is a lack of information for production problems. End users or clients are sometimes a bit sloppy with providing information that's needed for a developer to understand why or what is causing exceptions. Normally this means the developer needs to spend extra time in first reproducing the problem and then solving it. In the blog I wrote a while ago I explained how Application Insights can help by solving exceptions. First by using the different search and filter capabilities to gather as much as data as possible. Secondly, how a specific user session can be analyzed which can help the developer to reproduce the exception locally. This is extremely helpful with production exceptions that only happen occasionally since these type of exceptions are difficult to trace. Lastly, I explained that you can push extra meta information to Application Insights that can help you understand or identify the cause. In this blog, I'll use the same example as in my previous blog so make sure you have read that blog first. I'll explain how snapshots provide all the data that you need to solve production exceptions fast and efficient.

What are snapshots?

Snapshots can be used when your application runs in Azure and Application Insights is enabled. Snapshots are taken when exceptions occur. It contains valuable information about the current state of your application when an exception occurred. That's not only the exception message and stack trace but also the state of all local variables at the location where the exception was thrown. Think of how easy it would be to reproduce an exception when you exactly know the values of all variables. A snapshot can be analyzed in two ways, either in the Azure portal or in Visual Studio. Of course, it's already extremely useful knowing all values of the variables but it's even better to do that directly in Visual Studio. What I mean is that you can debug a snapshot. It's like debugging your application locally and an exception is thrown. Pretty awesome if you ask me. In the next section, I'll explain how you can enable snapshots in your Visual Studio solution.

Configure snapshots in your Visual Studio solution

The snapshot feature can be enabled and configured directly in Visual Studio. The first step would be to install the Microsoft.ApplicationInsights.SnapshotCollector NuGet package. This package will collect all information from the current state of the application and create snapshots when exceptions are thrown. Snapshots can be investigated both in the Azure portal online and in Visual Studio. To be able to open a snapshot (.diagsession) in Visual Studio you need to install the Microsoft Snapshot Debugger extension. I'll discuss later what you can do with a snapshot in Visual Studio. Lastly, snapshots can be configured in the applicationinsights.config. The table below shows an overview plus explanation of all possible configurations.

IsEnabled Enable or disable the snapshots feature. IsEnabledInDeveloperMode Enable or disable the snapshots feature while your application is in developer mode. ThresholdForSnapshotting How many times an exception needs to occur within an interval (configuration 'ProblemCounterResetInterval') before a snapshot is taken. MaximumSnapshotsRequired Maximum number of snapshots that will be created for a specific exception within 24 hours. MaximumCollectionPlanSize Number of exceptions that can be monitored at any time. Keep in mind that the package keeps in memory how often an exception is thrown and whether or not a snapshot needs to be created. ProblemCounterResetInterval The package keeps in memory how often a specific exception occurs. This configuration will reset these counters at an interval. SnapshotPerMinuteLimit How many snapshots max can be taken every minute. SnapshotsPerDayLimit How many snapshots max can be taken every day.

Note in order to make proper snapshot you'll need to publish the .pdb files to your web app. Also, make sure that optimize build is not enabled since this will affect the availability of the local variables. Microsoft says that in future releases this will be fixed.

Web application exception

What I already mentioned in the introduction is that I'm using the same demo application that I used in this blog. In short, visitors can do a registration in the demo application. The registration form changes based on the category selection that can be done on a different page. When a visitor selects the category 'car' and the registration form is submitted an exception occurs. This doesn't happen when the visitor selects a different category and submits the registration form. Since this exception only happens occasionally it can be hard to reproduce. In the previous blog, I explained that you can investigate the telemetry data for specific user sessions. That will reveal the information that's entered by the visitor and triggered the exception. In this blog, I'll use snapshots to solve the same exception. Below an animation of the problem.