Azure ApplicationInsights track custom metrics using the TelemetryClient

The Microsoft.ApplicationInsights nuget package is your code gateway to the Microsoft Application Insights portal, and also to the Azure Monitor universe. You use these tools as a shared place for your log data, telemetry, alerts, diagnostics etc.

The Application Insights comes standard with a vast number of metrics:

Application Insights Standard Metrics

Application Insights Standard Metrics

But you can easily create any metric of your choice.

STEP ONE: CREATE A TELEMTRYCLIENT INSTANCE

First you configure a TelemetryClient instance. The InstrumentationKey is the key to your Application Insights instance:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace MyCode
{
  public static class ApplicationInsights
  {
    public static TelemetryClient Initialize()
    {
      TelemetryConfiguration configuration = TelemetryConfiguration.Active;
      configuration.InstrumentationKey = "the-application-insights-key";
      return  new TelemetryClient(configuration);
    }
  }
}

STEP 2: LOG CUSTOM METRICS:

This is a one-line-of-code operation:

public static void TrackProcessMessageCount(TelemetryClient client, int count)
{
  client.GetMetric("Process message count").TrackValue(count);
}

The new custom metric is automatically placed in a metric namespace called “azure.applicationinsights“.

The code is used like this:

var TelemetryClient client = ApplicationInsights.TelemetryClient.Initialize();
TrackProcessMessageCount(client, 200);

You can now create a dashboard with your own custom metrics. In this example I have 3 custom metrics, and I have chosen a bar chart:

Application Insights Metrics

Application Insights Metrics

That’s all there is to it.

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, Microsoft Azure and tagged , , , , . Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.