Debugging is one of the most powerful tools that you, as a developer, have to fix an issue. Unfortunately, debugging is usually only possible locally, or at best, in an environment that is not production. This is because when you debug an app and hit a breakpoint, the app pauses and users can’t use it anymore until you release the breakpoint.
There is a better way. Now, in Azure, you can use the Snapshot Debugger to debug applications in production, without interrupting them.
The Snapshot Debugger works by taking a ‘snapshot’ of your application at a certain point in time. Taking a snapshot is like hitting a breakpoint, but once the snapshot is taken (which happens in 10-20ms), the application continues to run like normal.
You can then examine the snapshot, just like you examine all of the variables, locals and test things out in the watch window, when you are debugging.
So the Snapshot Debugger allows you to actually debug in production, which will help you to fix issues.
Let’s take a look at how to use the Snapshot Debugger:
Using the Snapshot Debugger from Visual Studio 2017
To use the Snapshot Debugger from Visual Studio, you need a couple of things:
- Visual Studio 2017 Enterprise (15.5 or up)
- The Visual Studio Snapshot Debugger Extension (download here)
- An ASP.NET or ASP.NET Core app running in Azure App Service (in a Web, Mobile, Function or API App)
- For this example, we’ll assume that there is an ASP.NET Core 2.0 website running in an Azure Web App
Here is how it works:
- In Visual Studio, go to the Cloud Explorer
- From here, you can right-click the Web App that runs your application and click Attach Snapshot Debugger
a. Attaching the snapshot debugger to a Web App for the first time will install a site extension that requires you to **restart the Web App**. Once that is done, you can use the Snapshot Debugger
Now, you can add a snap point to a line of code, just like you would add a breakpoint when debugging
After that, you click the Start Collection button at the top. This will monitor the line of code, just like a breakpoint would do. And when it is hit, it takes a snapshot
a. Make sure that the code that you have running is the same as the code that is running in your Web App
- Now, go to the webpage and navigate to whatever page you need to hit your snap point
- This will generate a snapshot, which appears in the Diagnostic Tools window. Click on View Snapshot to open it
- Once the snapshot is loaded, you get the same experience as when you are debugging and hit a breakpoint. You have access to the full stack trace, local variables and you can even use the watch window
That’s it! Using the Snapshot Debugger kind of reminds me of using IntelliTrace back in the day, as it also provided sort of a snapshot that you could use later on, although the Snapshot Debugger is way more easy to use.
Using the Snapshot Debugger to write log messages
In addition to creating debugging snapshots, you can also write messages to your Output Window or Application Log by using snap points. This requires no change to your application and works in a similar way to using snap points for debugging snapshots:
- In Visual Studio, when you have created a snap point, right click on it and choose Actions
- Now, you can create a message which will be sent to the Output Window and/or the Application Log
- When you are done, click the Update Collection button in the top to send the changed snap point to Azure and start logging
Now, whenever the snap point is hit, your message gets logged. Either to the Output Window in Visual Studio, when you have the Snapshot Debugger attached, or to the Application Logs, which you can read anywhere where you can read System.Diagnostics.Trace messages, like in Application Insights.
The Snapshot Debugger enables you to literally debug in production, which is great! It also enables you to add log messages easily, without changing your app, which is very powerful.
Snapshot Debugger is functionality in Azure that you can attach to with tools like Visual Studio 2017 Enterprise and Application Insights and it is currently in preview.
I think that you need the Visual Studio experience to really get value out of Snapshot Debugger, which requires you to have the enterprise edition, which is expensive and not available for everybody. If you have it though, use this awesome feature, because it can help you a lot.
Let me know what you think in the comments.