Exception Logging and Tracking with Application Insights 4.2

After finally getting the latest version of App Insights Extension installed into Visual Studio, its been a breath of fresh air to use.

Just a note, to get it installed, I had to go to to installed programs, hit modify on VS2015, make sure everything else was updated. Then run the installer 3 times, it failed twice, 3rd time worked.

Now its installed I get a new option under my config file in each of my projects called “search”.


This will open the Search window inside a tab in visual studio to allow me to search my application data. The first time you hit it you will need to login and link the project to the correct store though, after that it remembers.


From here you can filter for and find exceptions in your applications and view a whole host of information about them. Including information about the server, client, etc. But my favorite feature is the small blue link at the bottom.


Click on this will take you to the faulting function, it doesn’t take you to the faulting line though (which i think it should) but you can mouse over it to see the line.


One of the other nice features, which was also in the web portal. is the +/- 5 minutes search.


You can use this to run a search for all telemetry within 5 minutes either side of the exception. In the web portal there is also an option of “all telemetry of this session”, which is missing from the VS interface, I hope they will introduce this soon as well.

But the big advantage to this is if you have setup up App Insights for all of your tracking you will be able to see all of the following for that session or period:

  • Other Exceptions
  • Page Views
  • Debug logging (Trace)
  • Custom Events (If you are tracking thinks like feature usage in JavaScript this is very handy)
  • Raw Requests to the web server
  • Dependencies (SQL calls)

Lets take a look at some of the detail we get on the above for my +/- 5 Minute view

Below is an SQL dependency, this is logging all my queries. So I can see whats called, when, the time the query took to run, from which server, etc. This isn’t any extra code I had to write, App Insights will track all SQL queries that run from your application out of the box, once setup.


And dependencies won’t just be SQL, they will also be SOAP and REST requests to external web services.

Http Request monitoring the detail is pretty basic but useful.


And Page views you get some pretty basic info also. Not as good as some systems i have seen, but defiantly enough to help out with diagnosing exceptions.


I’ve been using this for a few days now and find it so easy to just open my solution in the morning and do a quick check for exceptions, narrow them down and fix them. Still needs another version or two before it has all the same features as the web interface, but defiantly worth a try if you have App Insights setup.




Application Insights, and why you need them

Metrics are really important for decision making. To often in business I hear people make statements based on assumptions, followed by justifications like “It’s an educated guess”, if you want to get educated you should use metrics to get your facts first.

Microsoft recently published a great article (From Agile to DevOps at Microsoft Developer Division) about how they changed their agile processes, and one of the key things I took out of it was the change away from the product owner as the ultimate source of information.

A tacit assumption of Agile was that the Product Owner
was omniscient and could groom the backlog correctly…

… the Product Owner ranks the Product Backlog Items (PBIs) and these are treated more or less as requirements. They may be written as user stories, and they may be lightweight in form, but they have been decided.

We used to work this way, but we have since developed a more flexible and effective approach.In keeping with DevOps practices, we think of our PBIs as hypotheses. These hypotheses need to be turned into experiments that produce evidence to support or diminish the experiment, and that evidence in turn produces validated learning.

So you can use metrics to validate your hypotheses and make informed decision about the direction of your product.

If you are already in azure its easy and free for most small to mid-sized apps. You can do pretty similar things with google analytics too, but the reason i like the App Insights is it combines server side monitoring and also it works nicely with the Azure dashboard in the new portal, so i can keep everything in once place.

From Visual studio you can simply right click on a project and click “Add Application Insight Telemetry”


This will bring up a wizard that will guide you through the process.


One gotcha i found though was that because my account was linked to multiple subscriptions i had to cancel out of the wizard, then login with the server explorer then go through the wizard again.

It adds a lot of gear to your projects including

  • Javascript libraries
  • Http Module for tracking requests
  • Nuget packages for all the ApplicaitonInsights libraries
  • Application insights config file with the Key from your account

After that’s loaded in you’ll also need to add in tracking to various areas, the help pages in the azure interface have all the info for this and come complete with snippets you can easily copy and paste for a dozen languages and platforms (PHP, C#, Phyton, Javascript, etc) but not VB in a lot of instances which surprised me 🙂 you can use this link inside the Azure interface to get at all the goodies you’ll need


Where I recommend adding tracking at a minimum is:

Global.ascx for web apps

public void Application_Error(object sender, EventArgs e)
// Code that runs when an unhandled error occurs

// Get the exception object.
Exception exc = Server.GetLastError;

dynamic telemetry = new TelemetryClient();


Client side tracking copy the snippet from their interface

Add to master page or shared template cshtml for MVC.



Also you should load it onto your VMs as well, there is a web platform installer you can run on your windows boxes that will load a service on for collecting the stats.



Warning though that the above needs a IISReset to complete. A good article on the setup is here.

For tracking events I normally create a helper class that wraps all my calls to make things easier.

Also as a lot of my apps these days are heavy JavaScript I’m using the tracking feature in JavaScript a lot more. The snippet they give you will create a appInsights object in the page you can reuse through your app after startup.

Below is an example drawn from code I put into a survey App recently, not exactly the same code but I will use this as an example. This is so we could track when a user answers a question incorrectly (i.e. validation fails), there is a good detailed article on this here

SurveyName: "My Survey",
SurveyId: 22,
SurveyVersion: 3,
FailedQuestion: 261

By tracking this we can answer questions like

  • Are there particular questions that users fail to answer more often
  • Which surveys have a higher failure rate for data input

Now that you have the ability to ask these questions you can start to form hypotheses. For example i make make a statement like;

“I think Survey X is too complicated for users and needs to be split into multiple smaller surveys”

To validate this I could look at the metrics collected for the above event for the user error rate on survey X compare to other surveys and use this as a basis for my hypotheses. You can sue the metrics explorer to create charts that map this out for you, filtering by you event (example below)


Then after the change is complete I can use the same metrics to measure the impact of my change.

This last point is another mistake I see in business a lot, too often changes is made, then no one reviews success. What Microsoft have done with their “Dev Ops” process embeds into their metrics into the process. So by default you are checking your facts before and after change, which is the right way to go about things imo.



Azure DNS Web UI, the beginning

I was a bit frustrated to hear that the Azure DNS service didn’t have a UI.

It is however understandable, the main purpose for introducing a DNS service in the cloud environment is primarily around environments that will be run up programmatically (i.e. Infrastructure as code).

But I’ve been meaning to dig my hands into a small sized angularJS project to try out some stuff so I decided to write a small Web UI for it, this is why I haven’t done any posts in a week. Well that and my cousin’s wedding 🙂

It is starting to Take shape now in GitHub:


It’s based on the Authentication libraries published in the TODO example app from MS.

There is still a bit of work to be done, i’ll be doing some follow-up posts, and hopefully be able to get it into the Azure Marketplace.

Currently there is a requirement of configuring an Azure Application in Azure AD, then putting the AppID and tenant in to the config to make it work. As I understand it, if it is a marketplace app that will configure this automatically in your environment when its installed.

This is configured under the active directory section in Azure, and there is a few gotchas i will cover in followup posts. but hopefully won’t be needed in the long run.


<add key="ida:Tenant" value="hostedsolutionscomau.onmicrosoft.com" />
<add key="ida:Audience" value="ffd940d1-3eed-425b-9ae9-fd0e9955db29" />

The app itself is pretty basic, after a bounce and back to Azure/MSLive to login, you come back with a token that is passed with the API calls from JavaScript. The Azure Management gear is all CORS enabled so it can be hosted on a different domain from the APIs and runs fine.

You simply select the Subscription then resource group and you get a list of domains in that resource group.


Then once a domain is selected you can edit the individual records.

I noticed with the schema of the JSON objects they are designed hierarchical, assuming that for each A record, for example, there will most likely be multiple IPs (i.e. DNS load balancing), so i have tried to design the UI to suite this.

The exception was the TXT records, if someone can explain to me why you would have multiple records for a single TXT value, I’d be glad to fix it up, but even looking at the RFCs left me saying WTF?


I think it will be another few weeks before this is completed and ready for the market place. But I think its a tool worth having, because while the “run up” of Infrastructure as Code deployments might be all command line based, sometimes its handy to be able to use UI to check your work and make minor “on the spot” fix ups for troubleshooting, rather than having to pour through command lines.

more to come in the coming weeks…