Application Insights – Part 2

In the last blog post, I talked about how to get started with Application Insights. If you have not checked that out then you can read about it here. In this post, I will talk about how you can log ‘Traces’, ‘Events’ and ‘Exceptions’ from your application.

There are two ways, you can log to Application Insights:

  1. JavaScript logging
  2. Server side logging

JavaScript Logging

Logging from JavaScript is a two-step process:

  1. Register
  2. Log

To register, you need to post a pre-defined code to Application Insights with appropriate INSTRUMENTATION KEY either at page level or Master Page level or _Layouts.cshtml in case of an MVC application. 

Step 1: Log in to https://portal.azure.com and navigate to Application Insights created in the previous blog post. Once you are at the Application Insights section, click on the Quick Start icon. You will land on the screen shown below:

Step 2: Click on the “Get Code to monitor my web pages’ link from the Section “Add code to monitor web pages”. Copy the code from the window appearing now, which will look similar to below image:

Your code will look something like this:

   

If you analyze the code sample carefully, you will find there are two parts of it. First part is till the line window.appInsights=appInsights;  and Second part is appInsights.trackPageView(); which tells the Application Insights to track the page view. If you don’t want to track the page view, then you can omit this line.

Step 3: There are different events that you can write to Application Insights. For example, Trace, Event, Exception, Metric, Dependency, Request, PageView. Once you have initialized the Application Insights then you can write these different events.

To write Traces, you can write appInsights.trackTrace(“Your message”); You can also pass an additional parameter to log your component name i.e. appInsights.trackTrace(“Your message”, “{ComponentName: ‘Your Component Name’}”);

The same goes with Events and Exceptions. You can log events by appInsights.trackEvent(“Your message”);

Events and Exceptions have 3 overloads methods. Both the other parameters except the first one take Dictionary.

Code:

appInsights.trackEvent

  (“WinGame”,

     // String properties:

     {Game: currentGame.name, Difficulty: currentGame.difficulty},

     // Numeric metrics:

     {Score: currentGame.score, Opponents: currentGame.opponentCount}

     );

 

appInsights.trackPageView

    (“page name”, “http://fabrikam.com/pageurl.html”,

      // String properties:

     {Game: currentGame.name, Difficulty: currentGame.difficulty},

     // Numeric metrics:

     {Score: currentGame.score, Opponents: currentGame.opponentCount}

     );

Important Points to Note:

  •  For Trace – the first parameter is the message to log and second is the named string values of type <string, string> you can use to search and classify trace messages.
  • Events will generally be logged for major achievements. For example, in the gaming world it is used to track when a user wins the game. There’s a limit of about 1k on the string length. (To send large chunks of data, use the message parameter of trackTrace.)
  • In case of Event and Exception – The second parameter is of dictionary <string, string> and third parameter will be of dictionary <string, double> type. 2nd parameter can be used to search for the event in Application Insights tool
  • If you have initialized Application Insights at the page level of application level, then every uncaught exception will automatically be logged for that page or application respectively. This applies even if you have not logged the exception using trackException() method of Application Insights.

Server Side Logging

The logic to use logging from Server side remains the same as explained above. Hence, I will not go much deep into it. To get a quick start you can select Application Insights checkbox while creating the Web Application or install Application Insights as Nugets package, Change the INSTRUMENTATION Key in the .config file added with yours and that’s it. You are done!

You need to use TelemetryClient class to initialize Application Insights and use its object to log Trace, Event, Exception etc.

For example:

private TelemetryClient telemetry = new TelemetryClient();

telemetry.TrackEvent(“WinGame”);

 

// Set up some properties and metrics:

var properties = new Dictionary <string, string>

   {{“game”, currentGame.Name}, {“difficulty”, currentGame.Difficulty}};

var metrics = new Dictionary <string, double>

   {{“Score”, currentGame.Score}, {“Opponents”, currentGame.OpponentCount}};

 

// Send the event:

telemetry.TrackEvent(“WinGame”, properties, metrics);

That will be all you need to do for Application Insights to track your application. You can perform analysis at the portal or in Power BI. Is that not cool!

Thank you very much for your time! Leave your comments/questions in the comments box and I will reply as soon as I can.

Have a good rest of your day!

References:

 

Leave a Comment

Your email address will not be published. Required fields are marked *