Getting a clear view using Application Insights - VSO2GO Episode 5

Posted by Alex Turner - September 08, 2015

header-picture

Welcome back, we are at the end of our 5 part series. In this, the final episode, we will show you how to add Application Insights into your application. The content of this blog can be applied to almost any application including applications hosted outside of Azure. Application Insights allows you to track events, metrics, page views, users, crashes, dependencies, perf counters, and response times. If you unfamiliar with Application Insights check out the information and video here.

Note: Applications, hosted in Azure or not, can benefit from Application Insights telemetry data. You can monitor android, iOS, OSX and Windows apps, ASP.NET or J2EE.

In the following steps I’m using the Visual Studio IDE and the Azure preview portal.

1. Open your solution in Visual Studio, then right click on your project and select the Add Application Insights Telemetry… menu item.

Episode5_image001

2. Either select an Application Insight instance in the dropdown or choose Configure Settings to rename the current one, if you choose. Then Click Add

Episode5_image002

3. Now open the _Layout.cshtml file in your project. Paste this code just before the ending head tag in your file. Then replace the instrumentationKey value with yours. The key can be found in the ApplicationInsights.config file in the project.

<script type="text/javascript">

var appInsights=window.appInsights||function(config){

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||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",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

}({

instrumentationKey: "PASTE YOUR KEY HERE"

});

window.appInsights=appInsights;

appInsights.trackPageView();

</script>

4. Run your application locally. Visual Studio will let you know the Application Insights instance is getting data for the first time. You can also view the raw telemetry being sent in the Output window of Visual Studio.

Episode5_image003

5. Click the Open Application Insights Link in Visual Studio. This will open a deep link to your instance in Azure. The dashboard will show the most current activity.

Episode5_image004

6. Stop the running application. Open the Home controller. Use the code below to create two telemetry events in the Index action.

TelemetryClient tc = new TelemetryClient();

//Generic Custom Event
tc.TrackEvent("Index Page custom event");

//Custom Event with String Double Metrics
Stopwatch watch = new Stopwatch();
watch.Start();

Random rnd = new Random();

int randomCount = rnd.Next(1, 13);

int randomInterval = rnd.Next(1, 2000);

Thread.Sleep(randomInterval);

watch.Stop();

//Telemetry Eample Custom Event Properties

var metrics = new Dictionary<string, double>();

metrics.Add("Random Count", randomCount);

metrics.Add("Random Sleep Value", randomInterval);

metrics.Add("Random Time Eslapsed Milliseconds", watch.Elapsed.Milliseconds);

tc.TrackEvent("Custom Event With Metrics", null, metrics);

7. Run your application locally again. When it opens in the browser, refresh the Home Index page several times. After sometime to load and compile the data, you should start to see the custom event counter in the bottom of the App Insights panel.

Episode5_image005

8. Click on the Custom Events block to open a view of the events in their own panel.

Episode5_image006

9. Clicking an event type in the Activity by Event Name table will open all instance of that event in a new pane.

Episode5_image007

Episode5_image008

10. Clicking on a single event will open a pane showing the event details and metrics we added.

Episode5_image009

I would like to make a very strong point before we close out this blog. I mentioned briefly that Application Insight can be used in solution hosted outside of Azure. Go back to the root panel for your App Insights instance and click on the Servers icon

Episode5_image010

The performance data you see here should be from your local system like mine below. Application Insights pick up all this telemetry data from my local system not the Azure host. Application Insights can begin to provide you with valuable data today, whether you solution are hosted in Azure, local datacenter or some third party. It will even work for your desktop applications!

Episode5_image011

In closing, I hope you’ve enjoyed our journey through the pieces of a real world azure application. Please let us know what you think. Best regards and look forward to your comments.

Alex Turner
Sr. Azure Consultant | InCycle Software
MCSD | Azure Solution Architect
Central Region
Alex.Turner@InCycleSoftware.com

 

Do you have an EA with Microsoft? You could be eligible for thousands of dollars in free ALM or Azure consulting! Contact us to learn how.


Topics: Blog, Azure


Recent Posts

InCycle Named Azure Data Explorer (ADX) Partner

read more

OpsHub & InCycle Help Top Medical Device Company Accelerate Innovation

read more

InCycle Continues to Lead, Recognized by Microsoft for Industry Innovation. Earns Impact Award.

read more