How to tune Application Insights for Web API

Application Insights is very powerful out of the box with no further configuration, but it can be tuned for Web API so it’s even more powerful.  Suddenly, with a couple extra nugets and a couple configuration tweaks, you can start logging traces within .NET libraries and getting a much more robust sense of what the application is doing – especially when something goes wrong.

The difference

This is a snapshot of AI request data by just installing AI without further updates:

 

AI-web-api-before-additional-config
Before logging tuning

This is a snapshot after we tune for Web API:

 

AI-web-api-after-additional-config
After logging tuning

Notice the difference in data logged and insight into what the application is thinking and doing (obviously, since the API for this example is so simple, it’s relatively benign).

A better example is exception logging.

AI-web-api-exception-before-additional-config
Before logging tuning
AI-web-api-exception-after-additional-config
After logging tuning

This is extremely important when trying to figure out a bug that only occurs, for example, in staging and nowhere else.  The amount of data you can pull out of AI is pretty astounding.  Also, it’s very, very simple.

So, let’s get to it.

Install a couple nugets

To get all this extra data you need two additional nugets:

  1. Microsoft.AspNet.WebApi.Tracing – Enables ASP.NET Web API tracing to use System.Diagnostics
  2. Microsoft.ApplicationInsights.TraceListener – A trace listener that will hook into System.Diagnostics output

Configuration changes

When you install Microsoft.AspNet.WebApi.Tracing, a little readme.txt will pop up telling you how to configure this additional logging.  The short of it is this:

  1. Open WebApiConfig.cs
  2. Add “config.EnableSystemDiagnosticsTracing();” in the Register(HttpConfiguration) method (I put it first in the method)

Summary

That’s it. As with all of AI, this was extremely painless.  You are now collecting a massive amount of more information for your debugging pleasure.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: