Getting Started with Application Insights for ASP.NET Core 1.0

Michael Freidgeim edited this page Mar 11, 2018 · 2 revisions

Note: this page is about ASP.NET Core 1.0. For current ASP.NET Core 2.0. see page Getting started for Asp.Net Core 2.0

Monitor your live ASP.NET Core applications with Visual Studio Application Insights. Application Insights is an extensible analytics platform that monitors the performance and usage of your live web applications. With the feedback you get about the performance and effectiveness of your app in the wild, you can make informed choices about the direction of the design in each development lifecycle.

Follow the details in this article to add Application Insights to your app. You'll make the following changes:

  • Create an Application Insights resource
  • Add Application Insights NuGet package dependency to project.json
  • Add Application Insights instrumentation key to the appsettings.json
  • Add Application Insights instrumentation code to the startup.cs
  • Add Application Insights JavaScript instrumentation to the _ViewImports.cshtml, _Layout.cshtml

If you prefer, you can use Visual Studio to perform the first three steps by using the "Add Application Insights" menu command on your web project. But you still need to make some changes manually.

Create an Application Insights resource

  1. Sign in to the Microsoft Azure portal. (Need to sign up?)

  2. Create a new Application Insights resource. (Click New, Developer Services, Application Insights). Select the ASP.NET web application type.

    Create a resource

  3. In your new resource, open the Essentials drop-down so that you can copy the Instrumentation Key - you'll need it shortly.

Add Application Insights NuGet package dependency to project.json

Add the following entry to the dependencies section.

  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0"

Verify the version number: get the latest from the Release page.

In case of .NET Core applications, if you run into restore errors with respect to application insights dependency, add "dnxcore50" and "portable-net45+win8" to the imports list (if it does not exist), under frameoworks section of project.json, as described below. See Migrating from DNX for more details.

    "frameworks": {
        "netcoreapp1.0": { 
            "imports": ["dnxcore50", "portable-net45+win8"]

Add the instrumentation key to the appsettings.json

Add the instrumentation key of your Application Insights web application resource to the ApplicationInsights section of the appsettings.json.

  "ApplicationInsights": {
    "InstrumentationKey": "11111111-2222-3333-4444-555555555555"

Add Application Insights instrumentation code to startup.cs

If you don't already have the code that parses appsettings.json file and initializes configuration variable, create Configuration variable:

public IConfigurationRoot Configuration { get; set; }

Then add the following dependency entries to your project.json if they are not defined there already.

    "Microsoft.Extensions.Configuration.Abstractions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json":  "1.0.0"

(Verify the version number.)

Then add the code that parses configuration if you don't have it already in the method Startup.

  // Setup configuration sources.
            var builder = new ConfigurationBuilder()

            Configuration = builder.Build();

In the method Startup make sure to set Application Insights settings overrides. Specifically, set developer mode to true in development environment:

if (env.IsDevelopment())
    builder.AddApplicationInsightsSettings(developerMode: true);

In the method ConfigureServices add Application Insights service. You'll need to add namespace Microsoft.ApplicationInsights.AspNetCore in the using list:


In the method Configure add Application Insights request and exception tracking middleware. Note that request tracking middleware should be added as the very first middleware in pipeline:

Note: If you're using version 2.0 you will not include app.UseApplicationInsightsRequestTelemetry(); and app.UseApplicationInsightsExceptionTelemetry();, since they are managed internally.

// Add Application Insights monitoring to the request pipeline as a very first middleware.

Exception middleware should be added after error page and any other error handling middleware:

// Add Application Insights exceptions handling to the request pipeline.

If you don't have any error handling middleware defined, just add this method right after UseApplicationInsightsRequestTelemetry method.

Add Application Insights JavaScript instrumentation to the _ViewImports.cshtml, _Layout.cshtml

(if present)

In _ViewImports.cshtml, add injection:

@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration 

In _Layout.cshtml, insert HtmlHelper to the end of <head> section but before any other script. Any custom javascript telemetry you want to report from the page should be injected after this snippet:


For Version 2.0

Do not include the lines mentioned above, those are for version 1 only.

In _ViewImports.cshtml, add injection:

@using Microsoft.ApplicationInsights.AspNetCore
@inject JavaScriptSnippet snippet

Then, in your _Layout.cshtml file (to the end of <head> section but before any other script).


Run your app and view your telemetry

  • Use Search Explorer to investigate individual requests, exceptions, log traces and other events.
  • Use Metrics Explorer to slice and dice metrics and counts such as response times, page views and user counts.
  • Analytics is a powerful query language that you can use to extract performance and usage data from your telemetry.
  • Set alerts to notify you when your app goes off-limits.

Get more telemetry

  • Monitor dependencies to see if REST, SQL or other external resources are slowing you down.
  • Use the API to send your own events and metrics for a more detailed view of your app's performance and usage.
  • Availability tests check your app constantly from around the world.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.