When using Azure Application Insights, it is helpful to identify the components of your application by setting the cloud_RoleName field for telemetry. This makes it easy to filter telemetry data to specific components, e.g. “api” or “worker”, such as in Live Metrics:

Example of cloud_RoleName selection in Live Metrics view

To do this, most search results suggest to implement a custom ITelemetryInitializer and initialize each telemetry item with a value from configuration. While that, of course, works, there is also a helpful but little-known class named AzureWebAppRoleEnvironmentTelemetryInitializer, which is part of the Microsoft.ApplicationInsights.WindowsServer namespace. It is included in both the Application Insights integration for ASP.Net Core as well as that for .Net Worker Services.

You just register it as a telemetry initializer with the service collection before calling the appropriate method to register Application Insights. Like so:

// in ConfigureServices of your ASP.Net Core Startup.cs
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>();
services.AddApplicationInsightsTelemetry();

// or for worker services, using the Microsoft.ApplicationInsights.WorkerService NuGet package
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>();
services.AddApplicationInsightsTelemetryWorkerService();

Then, on your host environment, just set the WEBSITE_CLOUD_ROLENAME environment variable and the value will be relayed to your telemetry. This works everywhere, but is especially helpful on Azure App Service, where this initializer will also collect additional information like the host URL.

The result in your telemetry should look something like this:

Example of cloud_RoleName in Logs view