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:

1.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.

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:

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

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

01.// Set up some properties:
02.var properties = new Dictionary <string, string>
03.   {{"game", currentGame.Name}, {"difficulty", currentGame.Difficulty}};
04. 
05.var measurements = new Dictionary <string, double>
06.   {{"Score", currentGame.Score}, {"Opponents", currentGame.OpponentCount}};</p>
07. 
08.// Send the event:
09.telemetry.TrackEvent("WinGame", properties, measurements);</p>

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.

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.

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.

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.

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

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