Custom Telemetry Events with TrackEvent in Microsoft Application Insights

This article is also available on the Microsoft TechNet Wiki.

This article won a silver medal in The Microsoft TechNet Guru Awards – May 2015.

Microsoft Application Insights is a new service in Microsoft Azure. It is currently available in the new Portal.

With Application Insights you can view telemetry data from your resources, find problems with your deployed apps, and improve availability. It is easy to configure, even for the novice.

Custom telemetry events can be sent from mobile devices, web pages, and from applications on a server. Events can carry debug messages, application information, or even detailed usage information.

This wiki article will show you how to pass custom TrackEvent method statements to Application Insights within a C# MVC web application.

Prerequisites

Application Insights Setup

Before we can use Application Insights within our MVC project, please see the following articles on how to create an Application Insights resource and how to setup Application Insights within an MVC project:

Creating Custom Events

To create custom telemetry events in Application Insights we need to use the TelemetryClient Class. This one class will give us access to all the methods we can use to track our app.

To utilize the class you call it like this:

var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();

Using IntelliSense on the namespace, we can see all the methods available to us. Some of the more popular ones are TrackEvent, TrackException, and TrackPageView. For this article we will focus on TrackEvent.

TechNet_AI_01

The TrackEvent method can accept a simple string if you want to do some localized debugging or it can accept properties and measurements as Dictionaries to log more complex events.

A simple TrackEvent could be something as plain as logging when a certain method has been entered:

var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
telemetry.TrackEvent("Loading HomeController-Index View");

It can also be as complex as saving the latest high score on a game application:

// Set up some properties:
var properties = new Dictionary <string, string>
{{"game", currentGame.Name}, {"difficulty", currentGame.Difficulty}};
var measurements = new Dictionary <string, double>
{{"Score", currentGame.Score}, {"Opponents", currentGame.OpponentCount}};

// Send the event:
telemetry.TrackEvent("WinGame", properties, measurements);

Viewing Telemetry Events

Once you have added your TrackEvent messages to your MVC application, click F5 to run the default application in the browser. Since we have added a TrackEvent method to the Home Controller, the message will be automatically sent as soon as the site loads. Once the site has loaded stop debugging by clicking Shift + F5.

Browse to your Application Insights resource on the Azure Portal to view the results. To do so you can either:

  • Right click the project name and select Open Application Insights.
  • Expand the ApplicationInsights.config file and double click the {Application Insights resource name} Overview file.

Once the Azure Preview Portal page appears, sign in to Microsoft Azure. Locate your Application Insights resource and open it.

Click the Search tile to open the Diagnostic Search blade.

TechNet_AI_02

Scroll down the list of events until you see the title CUSTOM EVENT beside the event date. This is where you will find your logged custom event.

TechNet_AI_03

If you want to find the event without scrolling or you want to see all events with the same name you can search using the string name you gave the event.

TechNet_AI_04

As well, you can view all the CUSTOM EVENT records using the built-in filters. Click the Filters button to launch the Filter blade. Select or de-select the filters until you have the filtered view you want. (Notice as well how the Event Types match those within the TelemetryClient Class.) The Diagnostic Search blade will display your filtered results.

TechNet_AI_05

Finally, clicking on any CUSTOM EVENT will display the Custom Event Properties so you can view more details.

TechNet_AI_06

Conclusion

In this article you were introduced to custom telemetry events in Application Insights. Specifically, we saw how to use the TrackEvent method to log simple but effective messages within Application Insights. Note that the steps to view telemetry events are the same for all TelemetryClient Class methods.

See Also

References

Advertisements

Using Microsoft Application Insights in an MVC application

This article is also available on the Microsoft TechNet Wiki.

This article also won a silver medal in The Microsoft TechNet Guru Awards – April 2015.

This article is also the TechNet Featured Article for May 2015.

Microsoft Application Insights is a new service in Microsoft Azure. It is currently in free preview and it is available in the new Portal.

With Application Insights you can view telemetry data from your resources, find problems with your deployed apps and improve availability. It is easy to configure, even for the novice.

This wiki article will show you how to use Application Insights within an MVC web application.

Prerequisites

Application Insights Tools

Before we create our MVC project we need to verify we have the correct version of the Application Insights Tools.

Open Visual Studio 2013.

Click on Tools > Extensions and Updates.

01

The Extensions and Updates dialog window will appear. Click Installed on the left hand menu.

Find Application Insights Tools for Visual Studio near the top of the list. Click on the package and confirm the Version number is 2.4 or higher.

02

NOTE: If you have version 1.3.2 installed then this is the deprecated package for working with Application Insights inside Visual Studio Online. To upgrade to the latest SDK, make sure you have Visual Studio 2013 Update 3 (or later).

Click the Close button to close the Extensions and Updates dialog window.

Create Project

Click File > New > Project. The New Project dialog box appears.

Click Visual C# under Templates and select Web. The Add Application Insights to Project check box will be visible on the right side of the dialog box.

NOTE: For this example I am using Visual C# and a web project. However, Application Insights also works with Store Apps templates, specifically Windows Apps and Windows Phone Apps. It does not currently support Universal Apps. As well, Application Insights also works with the corresponding Visual Basic project types.

03

NOTE: If Application Insights is unavailable, make sure you have Visual Studio 2013 Update 3 (or later), that the tools exist under Extensions and Updates, and that you have selected a valid project template.

Click the Add Application Insights to Project check box. The Sign in to Visual Studio dialog box will appear. Enter your credentials and click Continue.

04

NOTE: Signing in is a two-step process. First, your email will be verified. Then the Sign in to your Microsoft account dialog box will appear. Enter your Password and click Sign in to finish the process.

Once your account has been verified the Add Application Insights to Project check box will become checked and your Azure account information will appear. As well, the Send telemetry to list will also be available,

05

The Use different account link allows you to switch from the currently logged in Azure account to another.

The Send telemetry to field details which Application Insights resource will house the telemetry data created by this MVC web application. The list defaults to creating an Application Insights resource based on the name of the project inside of a default resource group. The paragraph below the Send telemetry to list details exactly what will happen.

Clicking the Send telemetry to list will display all available Application Insights resources for this subscription. (Please see the following article for information on how to create an Application Insights resource: http://social.technet.microsoft.com/wiki/contents/articles/30264.creating-a-microsoft-application-insights-resource.aspx)

Selecting an Application Insights resource that is already created will update the text below the list accordingly.

06

Clicking the Configure Settings link will raise the Configuration Settings for Application Insights dialog box. This dialog box allows for more fine grain control of both the Resource Group and Application Insights Resource settings, For example, if I wanted to continue using an existing Resource Group but needed to create a new Application Insights Resource based on the project name then I could do the following:

07

Once the Application Insights settings are finalized, give the project a Name and click OK.

On the second screen select the MVC project template. Make sure the Host in the Cloud check box is unchecked.

08

Click OK.

The project will be provisioned and the Application Insights package will be added to the project. To confirm that Application Insights exists in the project an ApplicationInsights.config file will now be available in the project’s Solution Explorer. As well, the References folder will house the appropriate files.

Existing Project

Application Insights can also be added to existing web or store applications.

Open a project you wish to add Application Insights to in Visual Studio 2013.

Right click the project name within the Solution Explorer and select Add Application Insights Telemetry.

09

The Add Application Insights to Project dialog box will appear. It contains all of the same settings fields as the one in the Create New project section.

10

Click the Add Application Insights to Project button once the settings are finalized. The Application Insights package will be added as if it were a new project.

Run Project

Hit F5 to run the default MVC application in the browser.

Notice that the Application Insights counter appears in the Visual Studio toolbar. As you click through the different pages of the default application the counter will rise. This is showing the developer the number of telemetry events being sent to the defined Application Insights resource. A toast message will also appear on the first time it is run containing a link to your configured Application Insights Resource in Microsoft Azure.

11

View Telemetry Data

Now that the telemetry data has been pushed to the Azure Portal it is time to view the results. There are three ways this can be done:

  • Click the Open Application Insights link in the Toast message which appears upon first running the application.
  • Right click the project name and select Open Application Insights.
  • Expand the ApplicationInsights.config file and double click the {Application Insights resource name} Overview file.

12

Once the Azure Preview Portal page appears sign in to Microsoft Azure.

Locate your Application Insights resource and open it.

The Application Health section will be at the top of the resource blade and the generated data from the project will appear in the Overview timeline charts.

Click one of the SERVER RESPONSE TIME, BROWSER PAGE LOAD TIME, or SERVER REQUESTS chart panes to launch the blade containing its metrics. You can continue to drill down further within each blade to retrieve more granular data.

13

NOTE: It may take a few minutes for data to appear. Be patient and refresh the page occasionally. If none appears then check your settings and re-run the project to generate new events.

Conclusion

As you can see from this article adding Microsoft Application Insights to your web projects is easy to do. The service provides in-depth metrics for your deployed applications allowing you to find performance issues or to diagnose problems quickly.

NOTE: Since Application Insights is in preview mode information in this document may change in the future.

See Also

Book Review – ASP.NET MVC 4 Mobile Websites Succinctly (Syncfusion Inc.)

ASP.NET MVC 4 Mobile Websites Succinctly

Author: Lyle Luppes

Paperback: 87 pages

Publisher: Syncfusion Inc. (2012)

Language: English

Formats: Amazon Kindle and PDF

Price: Free

I was curious to read ASP.NET MVC 4 Mobile Websites Succinctly as the title really intrigued me. I thought it was solely about creating mobile websites using the Mobile Application template in ASP.NET MVC 4. And while that is an option that is mentioned in this book it is really about more than that.

Author Lyle Luppes takes the reader through the creation of a mobile website while using the Internet Application template and Razor view engine. This template comes with the following meta tag built into the Layout pages:

<meta name="viewport" content="width=device-width" />;

By using functionality that Microsoft provides right out of the box we can see immediately how easy it is to create a new mobile website or to add mobile support to an existing one.

Throughout the book we build out our MVC mobile site and the majority of this done by adding jQuery Mobile options to our website. If you are unfamiliar with jQuery then this book will do little to get you started. However, if you have used it in the past and are familiar with the syntax then this book gives a good overview of what is available to developers who use jQuery Mobile.

One of the strengths of the book is also one of its weaknesses. The text has lots of examples of what you can do with MVC 4 and jQuery Mobile. However, they mostly focus on the Apple line-up of products, especially the iPad and iPhone. The copy could have done with more mention of things to look out for when developing for other platforms – especially Android and Windows Phone.

Overall the book is an excellent introduction to incorporating jQuery Mobile into your MVC 4 applications so that you can easily make your websites mobile friendly. I know that I will refer to it again over the coming months to use many of the tips mentioned in this book within my future projects.

Visit the Syncfusion website to download ASP.NET MVC 4 Mobile Websites Succinctly or to see the entire Succinctly Series.

Disclaimer: In the interest of full disclosure I have received compensation from Syncfusion for this book review. However, Syncfusion had no editorial control over the writing of this blog post and did not instruct me on what I could or could not say in my review. In fact I have been meaning to read the Syncfusion books for some time and their offer was the impetus I needed to get started,