Created
February 6, 2023 10:13
web api program.cs to configure app insights with managed identity
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
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