Sitecore use Application Insights Metrics and Telemetry

Although Sitecore have an integration for Application Insights (which is part of Azure Monitor), you can implement the TelemetryClient yourself. This is useful if you wish to log Metrics or Trace your own code, without involving the complete Sitecore solution. There is no need for configuration changes if all you want is to get some metrics for your own subsystem within Sitecore.

Application Insights is very easy to work with. First you need to install the Microsoft.ApplicationInsights NuGet Package.

In order to use Application Insights you must create a TelemetryClient:

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

namespace MyCode
{
  public static class TelemetryFactory
  {
    /// <summary>
    /// Create a new TelemetryClient
    /// </summary>
    /// <param name="instrumentationKey">The Application Insights key</param>
    /// <param name="roleInstance">The name of your instance</param>
    /// <param name="roleName">The name of your role</param>
    /// <returns></returns>
    public static TelemetryClient GetTelemetryClient(string instrumentationKey, string roleInstance, string roleName)
    {
      var telemetryConfiguration = new TelemetryConfiguration {InstrumentationKey = instrumentationKey};
      telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
      telemetryConfiguration.TelemetryInitializers.Add(new CloudRoleInitializer(roleInstance, roleName));
      return new TelemetryClient(telemetryConfiguration);
    }
  }
}

With the TelemetryClient in hand, it’s easy to track metrics:

TelemetryClient _telemetryClient = 
  TelemetryFactory.GetTelemetryClient("my-key", "Application Name", "Application Name");

_telemetryClient.GetMetric(
  new MetricIdentifier("MySystem", $"Deleted Users")
).TrackValue(1);

The tracked telemetry will appear in Application Insights shortly after:

Application Insights Metrics

Application Insights Metrics

If you wish to log traces, it is equally easy:

var trace = new TraceTelemetry
{
  SeverityLevel = SeverityLevel.Information,
  Message = $"xxx Deleted User xxx"
};

_telemetryClient.TrackTrace(trace);

The traces appear as log lines in Application Insights:

Application Insights Trace

Application Insights Trace

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, Sitecore 7, Sitecore 8, Sitecore 9 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.