Adding Application Insights to a Windows 10 Universal App

Adding Application Insights to a Windows 10 Universal App

Application Insights helps us to track applications health in real time. You can track you application availability, performance issues, users session and diagnose crashes faster then ever. It’s provides a real time dashboard  for your app to monitor your application. Let’s have a look how to Create an Application Insights using new Azure Portal and then using it for your Windows Universal App. To start with  Application Insight in your Windows Universal app does not even require single line of code, you can just follow the below steps and start tracking your apps telemetry information.

To start with it, Let first learn how to “Create an Application Insight” in Azure Portal .The steps are pretty much simple and easy to followed.

First, Login in to Azure Portal and then navigate to New –> Developer Services –> Application Insights 

Creating New Application Insights Services in Azure Portal
Creating New Application Insights Services in Azure Portal

Then, provide Name of the “Application Insights” and Select the Application TypesWindows Store Application” . You can also change the resource group and location depends on your need. In this case we are not changing it.

image
Creating New Application Insights Services in Azure Portal

Once this is done, click on the “Create” Button. Make sure if you select “Pin to Startboard” checkbox selected, this will ensure you have a quick access icon on the Dashboard.

 

Creating New Application Insights Services in Azure Portal - Pin to Dashboard
Creating New Application Insights Services in Azure Portal – Pin to Dashboard

Once you select “Create” you should be able to see the creation is in process (left ) and  then once it’s created successfully, you should be able to see similar tiles  (right) in your dashboard.

image image

The details view for your Application Insights would looks like below . All the sections are clickable, you can select them and navigate to details.

image
Creating New Application Insights Services in Azure Portal – First Look

That’s all about creation of application insights. You really don’t need to do anything here as of now. Your application insight is ready !

Let’s start creating a new app and see how do we enable the application insight for the app.

Start a new instance of Visual Studio 2015 –> Add New Project –> Select “Blank App (Universal Windows) Template.

In the right side, select the checkbox for “Show telemetry in the Windows Dev Center” and “Enable richer analytics with Application Insights”.  When you select this, you will ask for login with your Microsoft Account, and Use the same account for which you have the Azure Subscription and created the Application Insights. Once you are authenticated with your  azure account, you should be able to see all your application insight here.

image
Creating New Universal Project and Selecting Application Insights

For our demonstration purpose, we will select the “MyWindowsUniversalApp”  and then Select “OK

During the Solution Creation, Visual Studio atomically takes care of all kinds of configurationcode registration and downloading nugget packagesimage

If you look inside the solution there are few additional components installed and configured to support integration with Application insights.

  • All required Nugget Packages
  • Application Insights Getting started documents
  • Application Insight Config

The Application Configuration files contains the Instrumentation Key, that is mapped to our created Application Insights.

image

When the applications starts, Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(); is called.

image

This code Initializes default configuration and starts automatic telemetry collection for application based on the provided Instrumentation Key

 

That’s all. Your application is ready for send automatic telemetry information.  If you run the app, you should be able to see following notification . If you click on the “Open Applications Insights” you would be redirected to the portal.

image

Just for demo purpose and simulate the app crash add one “Button” in the MainPage.xaml and write following code block on the button click.

image

Then Run the app and click on the button.  Your app will throw an exception.

Let’s open the Azure Portal  –> Application Insights Dashboard , You should be able to view all the details of your applications, including the crashes that generated.

Application Insights - Dashboard
Application Insights – Dashboard

You can do further deep down to see the error details.

image
Application Insights – Dashboard

So far  the app was running from the desktop,  now if you run the same app in a mobile simulator , you should be able to see multiple devices in the dashboard.

Application Running on Mobile Device
Application Running on Mobile Device

image

Even you can see which devices causing the maximum of crash through the Application Insights dashboard.

image
Application Crashes Details Device Specific

This was just a simple example and but definitely it will get you started with Application Insights and you can then explore more from here..

Hope this helps !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.