Azure Application Insights – Multiple services from same server

This trick enables you to have multiple services from the same server log to the same Application Insights instance, and at the same time distinguish between each service.

Application Insights

Application Insights Telemetry data

In the example above, I have 2 services from the same server. Each service have their own instance name. The @LTBP is a web server, and the @CloudDataloggerV1 is a windows console application.

To do this, you need to add a new TelemetryInitializer that sets the RoleInstance name.

STEP 1: CREATE NEW TELEMETRYINITIALIZER

This simple class adds the name of my service to the RoleInstance property:

internal class CloudRoleNameInitializer : ITelemetryInitializer
{
  private readonly string _roleName;

  public CloudRoleNameInitializer()
  {
    _roleName = "CloudDataLoggerV1";
  }

  public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
  {
    telemetry.Context.Cloud.RoleInstance = _roleName;
  }
}

STEP 2: ADD THE TELEMETRYINITIALIZER TO THE APPLICATIONINSIGHTS INSTANCE

You can now add the CloudRoleNameInitializer to your ApplicationInsights initializing code. This example is taken from the console application’s setup code:

public static class ApplicationInsights
{
  private static TelemetryClient _telemetryClient;

  public static void Initialize()
  {
    TelemetryConfiguration configuration = TelemetryConfiguration.Active;
    configuration.InstrumentationKey = Settings.ApplicationInsights.InstrumentationKey;
    configuration.TelemetryInitializers.Add(new CloudRoleNameInitializer());
    QuickPulseTelemetryProcessor processor = null;
    configuration.TelemetryProcessorChainBuilder
      .Use((next) =>
      {
        processor = new QuickPulseTelemetryProcessor(next);
        return processor;
      })
      .Build();

    var QuickPulse = new QuickPulseTelemetryModule();
    QuickPulse.Initialize(configuration);
    QuickPulse.RegisterTelemetryProcessor(processor);

    _telemetryClient = new TelemetryClient(configuration);
    Telemetry = _telemetryClient;
  }
  public static TelemetryClient Telemetry { get; private set; }

}

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 c#, General .NET, 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.