Application Insights is a preview service offered as part of Visual Studio Online. If you don’t have access to the preview yet then keep an eye on Brian Harry’s blog for updates.
If you are lucky enough to have access and you are building a Windows Phone or Windows 8 application, then you can use this quick guide to allow you to track crashes in your apps.
Automatic exception logging in apps isn’t (yet?) a feature of Application Insights, but its perfectly possibly to use the in-build event logging functionality to do this. To make this happen you need to have first setup Application Insights for your app. I’m going to assume you’ve done this already as you’ve got this far. If you haven’t then check out these articles on MSDN on how to do so for Windows Phone and Windows Store Apps and then come back here.
The logging API for Application Insights is really simple.
This will log an event “MyEvent” that will be categorised under the title “MyCategory”. You may have many event categories that you want to log across your app and you unhandled exception events will just be another category. You can choose what you want to call it, but for this example we are just using ‘Exceptions’ as the category name. You’ll need to call this code from two places, the UnhandledException handler and the UnobservedTaskException handler although you can use a different category name to separate these out.
The event name is slightly more problematic. You want to have a unique event captured for each execution path that leads to an exception. In order to do this you can generate a hash based on the stack trace of the exception. Don’t forget that, in this world of async/await and AggregateException, you’ll need to include the child elements of the AggregateException and their InnerException as well.
The following method should give you a string that when hashed results in a unique id by which to classify your exception.
By calling this method where you handle your exceptions you can use it as the event id when you call the logging code.
The method above is doing three things.
- Calling out first method to generate a unique and complete stack trace, we are then hashing this and using this as our identifier in 3.
- Adding dimensions for StackTrace, Type and Message, along with the HashCode. This allows us to extract the information we need to identify what is causing the crash.
- Constructing the ‘eventMessage’ and logging the event with Application Insights.
You’ll see at the end where we construct the ‘eventMessage’ variable, the event log path is split into 2 categories and an identifying event. Here we are further splitting the events down by the appVersion. This makes it easier for us to focus on where the issues are in the current version of the app. Take a look at the screenshot below which makes this clearer.
Hopefully this gives a useful insight in to how to instrument your Store Apps so as to be able to track and fix the exceptions that are occurring in them.