One way to improve our Application Insights telemetry, is by setting the cloud role name yourself. I shared how to do this in an ASP.NET application in a previous post. In this post I'll show you how to use this approach in an ASP.NET Core application.
The first steps are quite similar. We still need to create a custom TelemetryInitializer. Therefore we create a class that implements the ITelemetryInitializer
interface and implement the Initialize
method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CloudRoleNameTelemetryInitializer : ITelemetryInitializer | |
{ | |
public void Initialize(ITelemetry telemetry) | |
{ | |
// set custom role name here | |
telemetry.Context.Cloud.RoleName = "FrontEndApp"; | |
} | |
} |
Now, in the Startup.ConfigureServices
method, we register that telemetry initializer as a singleton:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSingleton<ITelemetryInitializer, CloudRoleNameTelemetryInitializer>(); |
Or if you are using .NET 6 minimal API’s:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddSingleton<ITelemetryInitializer, CloudRoleNameTelemetryInitializer>(); |