Application Insights: How to set the cloud role name in .NET Core & .NET 5

Wouter
2 min readDec 11, 2021

PS: I wrote a similar article on how to set the Cloud Role name in Angular.

If you are hosting your .NET solution in an Azure App Service this property is filled in for you, but in all other cases you need to set this property yourself. This can be done by creating your own Telemetry Initializer.

Create a custom Telemetry Initializer

Create a new class that inherits from the ITelemetryInitializer interface. Because my telemetry initializer is setting the Cloud Role Name I’m calling it CloudRoleNameTelemetryInitializer.

public class CloudRoleNameTelemetryInitializer : ITelemetryInitializer

In the initialize method you can set your Cloud Role Name by assigning it to the telemetry context. Like this:

telemetry.Context.Cloud.RoleName = "Your app name"

Your class should look somewhat like this:

Register your Telemetry Initializer in your startup

Next we need to register our class so it is used by Application Insights. In your startup.cs, right under the add AddApplicationInsightsTelemetry(); line add:

services.AddSingleton<ITelemetryInitializer>(new CloudRoleNameTelemetryInitializer("Your application name"));

See the result in Azure

When you open the Application Insights transaction search page you should now see your application appear in the dropdown list. And you should be able to search for transactions.

And that’s it! That’s all you need!

--

--