Tag Archives: AI

Application insights for Windows Phone and Store apps

Azure Application Insights lets you monitor your published Mobile apps

Requirements:

  • A subscription to Microsoft Azure
  • Visual studio 2013 or later

Create an Application Insights resource

Log in to your azure portal.

In the azure portal, create a new Application Insights resource.

clip_image002

Select Application Type as Windows Phone Application (here you can choose in whichever the application you want to implement Application Insights, In my case it is Windows Phone Application)

clip_image004

Click on Create Button, It will create the Application Insights resource. A resource in Azure is an instance of a service. This resource is where telemetry from your app will be analyzed and presented to you.

Copy the Instrumentation Key

The key identifies the resource. You’ll need it soon, to configure the SDK to send the data to the resource.

clip_image005

Add the Application Insights SDK to your apps

In Visual Studio, add the appropriate SDK to your project.

If it’s a Windows Universal app, repeat the steps for both the Windows Phone project and the Windows project.

Right-click the project in Solution Explorer and choose Manage NuGet Packages.

clip_image006

1. Select Online, Include prerelease, and search for “Application Insights”.

clip_image008

2. Pick the latest version of the appropriate package – one of:

  • Application Insights for Windows applications – for Windows Phone and Store apps
  • Application Insights for Web Apps
  • Application Insights API – for Windows desktop apps

3. Edit ApplicationInsights.config (which has been added by the NuGet install). Insert this just before the closing tag

Code Snippet
  1. <InstrumentationKey>the key you copied</InstrumentationKey>

Windows Universal apps: Repeat the steps for both the Phone and the Store project.

clip_image010

Enable network access for your app

If your app doesn’t already request outgoing network access, you’ll have to add that in the manifest as a required capability.

clip_image012

Run your Project

Run your application with F5 and use it, so as to generate some telemetry.

In Visual Studio, you’ll see a count of the events that have been received.

clip_image014

See Monitor data

Open Application Insights in your azure portal you just created you will be able to see some data in your Application Insights dashboard

clip_image015

Track Usage

From the Overview timeline, click through Users and Sessions charts to see more detailed analytics.

Users are tracked anonymously, so the same user on different devices would be counted twice.

A session is counted when the app is suspended (for more than a brief interval, to avoid counting accidental suspensions).

Page Views

To discover the paths that users follow through your app, insert page view telemetry into your code:

Code Snippet
  1. var telemetry = new TelemetryClient();
  2.  
  3.             telemetry.TrackPageView("HubPage");

See the results on the page views chart, and by opening its details:

clip_image017

Click through any page to see the details of specific occurrences.

Custom Events

By inserting code to send custom events from your app, you can track your users’ behavior and the usage of specific features and scenarios.

For example:

Code Snippet
  1. var telemetry = new TelemetryClient();
  2.             telemetry.TrackEvent("AppStarted");

The data will appear in the Custom Events grid. You can either see an aggregated view in Metrics Explorer, or click through any event to see specific occurrences.

clip_image019

If you want you can also set up some properties and metrics along with the TrackEvent(). Below code explains how to set up properties and metrics.

Code Snippet
  1. var properties = new Dictionary<string, string> { { "IsSuccess", result.IsSuccessStatusCode.ToString() }, { "StatusCode", result.StatusCode.ToString() },
  2.                                                                     { "PlanogramId", completeOrder.PlanogramId.ToString() } };
  3.                 var metrics = new Dictionary<string, double> { { "RestockOrderAPI Process Time", stopwatch.Elapsed.TotalMilliseconds },
  4.                                                                 { "Products Count", completeOrder.linesList.Count() }};
  5.                 App.telemetry.TrackEvent("RestockOrderAPI", properties, metrics);

So, when you go to the dashboard in the Custom Events  you will ale to see the track event you created by the code and you will able to see the properties and metrics attached with the track event api.

Screenshot (218)

 

And if you want to see the attached properties of that particular event just click on it view more information.

Screenshot (219)

 

Track Trace

Use this to help diagnose problems by sending a ‘breadcrumb trail’ to Application Insights. You can send chunks of diagnostic data, and inspect them in Diagnostic search.

 

Code Snippet
  1. var properties1 = new Dictionary<string, string> { { "message", result.RequestMessage.ToString() } };
  2.                 App.telemetry.TrackTrace("RestockOrderAPI Track Trace", properties1);

If you want to see the trace results, just open your AI resource and click on Diagnostic Search there you can see the number of traces has occurred.

Screenshot (220)

Screenshot (222)

Detecting Exceptions

Exceptions are reported to Application Insights by calling TrackException()

Code Snippet
  1. var telemetry = new TelemetryClient();
  2.  
  3.             try
  4.             {
  5.                 //put your logic here
  6.             }
  7.             catch (Exception ex)
  8.             {
  9.                 // Send the exception telemetry:
  10.                 telemetry.TrackException(ex);
  11.             }

 

 

Debug vs Release mode

Debug

If you build in debug mode, events are sent as soon as they are generated. If you lose internet connectivity and then exit the app before regaining connectivity, offline telemetry is discarded.

Release

If you build in release configuration, events are stored in the device and sent when the application resumes. Data is also sent on the application’s first use. If there is no internet connectivity upon startup, previous telemetry as well as telemetry for the current lifecycle is stored and sent on the next resume.

Monitor Crash Frequency

If your users experience crashes in your app, you’d like to know about it quickly, and you’d like details about what happened. With Application Insights, you can monitor how frequently crashes occur, get alerts when they occur, and investigate reports of individual incidents.

“Crash” means that the application terminates because of an uncaught exception. If your app catches an exception you can report it with the TrackException() API but continue running. In that case, it will not be logged as a crash.

clip_image020

Set an alert to detect crashes

clip_image022

Click on Alert Rules and then Alert Rules menu will open, in that select Add Alert

clip_image024

Diagnose crashes

To find out if some versions of your app crash more than others, click through the crashes chart and then segment by Application Version:

clip_image026

To discover the exceptions that are causing crashes, open Diagnostic Search. You might want to remove other types of telemetry, to focus on the exceptions:

clip_image028

Click any exception to see its details, including associated properties and stack trace.

clip_image030

See the other exceptions and events that occurred close to that exception:

clip_image032

Using Diagnostic Search in Application Insights

When do you see Diagnostic Search?

You can open diagnostic search explicitly:

clip_image034

Inspect individual items

Select any telemetry item to see key fields and related items. If you want to see the full set of fields, click “…”.

clip_image036

Filter event types

Open the Filter blade and choose the event types you want to see. (If, later, you want to restore the filters with which you opened the blade, click Reset.)

clip_image038

Filter on property values

You can filter events on the values of their properties. The available properties depend on the event types you selected.

clip_image040