Skip to content

Instantly share code, notes, and snippets.

@JsAndDotNet
Created October 7, 2022 10:27
Show Gist options
  • Save JsAndDotNet/05d504c50df4effb47b3457e9e4d4306 to your computer and use it in GitHub Desktop.
Save JsAndDotNet/05d504c50df4effb47b3457e9e4d4306 to your computer and use it in GitHub Desktop.
App Insights Set Cloud Role Name - Multiple Microservices off one App Insights (one workspace too)
// 0. Create Application insights (workspace version - You could set up a 'log analytics workspace' first)
// 1. In web app, Nuget -> Microsoft.ApplicationInsights.AspNetCore
// 2. Config (can either pass in app insights key, or use whole connection string in config)
// 3. (if using connectionstring, appsettings.json looks like)
//"ApplicationInsights": {
// "ConnectionString": "InstrumentationKey=...;IngestionEndpoint=https://...-0.in.applicati...ights.azure.com/;LiveEndpoint=https://........livediagnostics.monitor.azure.com/"
// },
// In another file
public class RoleNameInitialiser : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Cloud.RoleName = "ThisMicroserviceName";
}
}
// In program.cs
builder.Services.AddSingleton<ITelemetryInitializer, RoleNameInitialiser>();
builder.Services.AddApplicationInsightsTelemetry();
//builder.Services.AddApplicationInsightsTelemetry(myAppInsightsKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment