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.


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);


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.


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.

3 Responses to Azure ApplicationInsights track custom metrics using the TelemetryClient

  1. Pingback: Sitecore use Application Insights Metrics and Telemetry | Brian Pedersen's Sitecore and .NET Blog

  2. Pingback: Application Insights not working in .NET Core Console Application – Remember to flush the TelemetryClient | Brian Pedersen's Sitecore and .NET Blog

  3. Pingback: C# Azure TelemetryClient will leak memory if not implemented as a singleton | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

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

You are commenting using your 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.