Enabling Application Insights for an ASP.NET Core web application

Posted: (EET/GMT+2)

 

Today's post is a quick note for wiring up Application Insights in an ASP.NET Core web application.

Here are the steps:

1) Add the package:

dotnet add package Microsoft.ApplicationInsights.AspNetCore

2) Register in Startup:

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry();
    services.AddMvc();
}

3) Set the instrumentation key (either appsettings.json or environment variable):

{
  "ApplicationInsights": {
    "InstrumentationKey": "YOUR-INSTRUMENTATION-KEY"
  }
}

If you prefer an environment variable instead:

setx APPINSIGHTS_INSTRUMENTATIONKEY "YOUR-INSTRUMENTATION-KEY"

That's it. Requests, exceptions, and basic dependencies start showing up in the Azure Portal. For custom events, inject a TelemetryClient and track as needed.