Web application metrics with Application Insight Part 2

I previously wrote about Azure Application Insights[^] in an article where I talked about how we would be using it within our ASP.NET Web API services. We use it in that context to monitor our services for such things as availability, performance and to record metrics such as the number of requests our services are processing. And Azure Application Insights gives me all of this, and a whole lot more besides.

This time round we were exploring logging engines for our latest ASP.NET Core 2.0 Razor Pages web application. We wanted something that would record the various events that our application would generate, as well as enable us to debug and diagnose errors and / or exceptions as they occurred.

We looked at various logging engines such as ELMAH and Log4Net. Each would have satisfied our requirements, but when we looked into the feature set of Application Insights, there was absolutely no comparison. Application Insights won the contest hands down. Straight out the box it measures practically everything you need without writing a single line of code. On top of that, you can write your own custom events, traces, exception handlers etc.

I’ve added several custom logging methods for monitoring and measuring our application whilst it’s running, as well as exception logging. All of this helps us to debug and diagnose issues, helps with development as we can add traces throughout the application, allows us to monitor and measure the health of the application and to generate exception reports when the application encounters errors.

Within the Azure portal, you can open your Application Insights blade and dice and slice this data any way you want. You can drill top down from your management summary type data (showing broad trends and metrics), right into the nitty gritty detail of an individual request or exception. The data can be filtered in an almost infinite number of ways and presented in multiple formats (or downloaded for offline use). And it’s fast. Despite the vast amount of data that is collected, filtering it and querying is surprisingly fast. Even complex queries of the data are returned blazingly fast.

To use Application Insights you firstly need to download the Application Insights package from nuget. Once installed, you can then start using it in your application.

Here’s a single example of a trace event I have implemented. I use it to trace the requests to our ASP.NET Web API services. We pass an event name, the duration of the request (so we can monitor for performance) and a dictionary of custom properties (which I use to pass the request arguments).

Hide Copy Code

public class LoggingService
{
private readonly TelemetryClient _telemetryClient = new TelemetryClient();
public void TrackEvent(string eventName, TimeSpan timespan, IDictionary<string, string> properties = null)
{
var telemetry = new EventTelemetry(eventName);
telemetry.Metrics.Add("Elapsed", timespan.TotalMilliseconds);
if (properties != null)
{
foreach (var property in properties)
{
telemetry.Properties.Add(property.Key, property.Value);
}
}
this._telemetryClient.TrackEvent(eventName, properties);
this._telemetryClient.TrackEvent(telemetry);
}
}

And here’s the method being invoked within the application.

Hide Copy Code

stopwatch.Restart();
try
{
//make an request to a service
var response = await new MyService().GetMyData(param1, param2);
}
catch (Exception ex)
{
service.TrackException(ex);
throw;
}
finally
{
stopwatch.Stop();
var properties = new Dictionary<string, string>
{
{ "param1", param1 },
{ "param2", param2 }
};
service.TrackEvent("GetMyData", stopwatch.Elapsed, properties);
}

You can add traces for events, exceptions, diagnostics etc. All of this data is recorded and available for you to filter, dice and slice in any way you need it.

If you’re looking for a logging engine in your application, then you need to check out Application Insights. It does everything we need and a whole bunch more, and is highly configurable and fast. The question is not why you should use it, but why you shouldn’t use it!

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store