Getting Started With Application Insights (AI)


Application Insights for Visual Studio Online lets you monitor your application by tracking events, exceptions, usage, counters and custom telemetry of your application.
Currently AI is in preview and available only on VSO, but you can jump and start using it Today!

Currently there is two versions of AI out there – the VSO version and Portal Version, if you’re using Visual Studio Update 3 or later, you’re probably using the portal version.
As I wrote AI is currently in preview and doesn’t provide all the information this kind of service should provide – OS, Browser, GEO and more analytic data – but in two weeks Microsoft should release a newer version with those metrics.


Before jumping to implementation let’s talk about the important stuff first – PERFORMANCE:
I did a tiny test for Google Analytics (12.6KB and 358ms loading time) and Microsoft AI (31.5 KB and 1.08s loading time) both for Web Apps, and the timing results below.



So Google Analytics is faster and lighter but Microsoft AI gives exceptions information and additional telemetry that essential for keep track my sites and apps.

Adding AI To Web Site \ Mobile (Cordova)

  1. 1. Login to Azure Portal –
  2. 2. Click “New” (Bottom Left) and choose “Application Insights


3. New blade will open where you need to specify the AI name, choose the subscription to assign, location and resource group, finally click Create.

4. After couple of seconds you should see the AI blade, click Quick Start and then “Get code to monitor my web pages”


You should see something like that:

1 <script type="text/javascript"> 2 var appInsights=window.appInsights||function(config){ 3 function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t 4 }({ 5 instrumentationKey:"d49d7db4-ae0b-4a09-a2d1-141726f8fa11" 6 }); 7 8 window.appInsights=appInsights; 9 appInsights.trackPageView(); 10 </script>

Notice that the code contains instrumentationKey that is unique identifier for AI, doesn’t require specific permissions or anything like that.

You can also locate the AI key from the Properties blade.

Now just add the above code inside your application head element and in seconds the data will be displayed on the Portal.


Now let’s add some custom telemetry to our app, first is trackEvent method:

1 window.appInsights.trackEvent("Main Page");

trackEvent receive object the represents string value for filtering and numeric for statistics.

First object called Properties are string values that you can use to filter your telemetry in the usage reports.  ( {Search: searchObj.productName} )

Measurements are numeric values that you can get statistics from in the usage reports. ( {Results: searchObj.results.length} ).

1 window.appInsights.trackEvent("Search Action", 2 {Search: searchObj.productName}, 3 {Results: searchObj.results.length}); 4 5 //Uploading Search Value - Take care not to log personally identifiable information in properties. 6 window.appInsights.trackEvent("Search Action - '" + searchObj.searchValue + "'");

For collecting timing data you should use startTrackEvent and stopTrackEvent:

1 window.appInsights.startTrackEvent(searchObj.jobId); 2 3 //Search Action..... 4 5 window.appInsights.stopTrackEvent(searchObj.jobId, 6 {Search: searchObj.productName}, 7 {Results: searchObj.results.length});

SPA and some in page navigation you might want to consider using trackPageView method.

1 window.appInsights.trackPageView("tab1"); 2 3 window.appInsights.trackPageView("tab1", "");

Adding AI To .NET Application

Open NuGet package manager and search for “Application Insights”, then install Application Insights API.


I’ve added the Thread.Sleep to make sure the TrackEvent method will finish successfully before the application is closed. (Using Console for this demo)

1 TelemetryClient client = new TelemetryClient(); 2 client.Context.InstrumentationKey = "b24d7db4-1ac6-4a09-b6d1-2fc731f8652c"; 3 4 try 5 { 6 client.TrackEvent("Starting Console"); 7 8 // Cool Stuff 9 } 10 catch (Exception ex) 11 { 12 client.TrackException(ex); 13 } 14 finally 15 { 16 client.TrackEvent("Closeing Console"); 17 } 18 19 Thread.Sleep(2000);

Add comment
facebook linkedin twitter email

Leave a Reply