Skip to content

Instantly share code, notes, and snippets.

@bhushang19
Created February 6, 2023 10:13
web api program.cs to configure app insights with managed identity
using Azure.Identity;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.Extensibility;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<TelemetryConfiguration>(config =>
{
var credential = new ManagedIdentityCredential();
config.SetAzureTokenCredential(credential);
});
builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = "InstrumentationKey=your-app-insight-resource-instru-key-guid;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/",
DeveloperMode = true
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment