Application Insights for ASP.NET Core

Azure Application Insights provides in-depth monitoring of your web application down to the code level. You can easily monitor your web application for availability, performance, and usage. You can also quickly identify and diagnose errors in your application without waiting for a user to report them.

This article walks you through creating a sample ASP.NET Core Razor Pages application in Visual Studio, and how to start monitoring with Azure Application Insights.

Prerequisites

  • NET Core 2.0.0 SDK or later.
  • Visual Studio 2017 version 15.3 or later with the ASP.NET and web development workload.

Create an ASP.NET Core project in Visual Studio

  1. Right-click and Launch Visual Studio 2017 as administrator.
  2. Select File > New > Project (Ctrl-Shift-N).

    Screenshot of Visual Studio File New Project Menu

  3. Expand Visual C# > Select .NET Core > ASP.NET Core Web Application. Enter a Name > Solution name > Check Create new Git repository.

    Screenshot of Visual Studio File New Project Wizard

  4. Select .Net Core > ASP.NET Core 2.0 Web Application > OK.

    Screenshot of Visual Studio File New Project Selection Menu

Add Application Insights Telemetry

  1. Select Project > Add Application Insights Telemetry... (Alternatively you can right-click Connected Services > Add Connected Service.)

    Screenshot of Visual Studio File New Project Selection Menu

  2. Select Start Free.

    Screenshot of Visual Studio File New Project Selection Menu

  3. Select an appropriate Subscription > Resource > and whether or not to allow collection over 1 GB per Month > Register.

    Screenshot of Visual Studio File New Project Selection Menu

Changes Made to your project

Application Insights is very low overhead. To review the modifications made to your project by adding Application Insights telemetry:

Select View > Team Explorer (Ctrl+, Ctrl+M) > Project > Changes

  • Four changes total:

    Screenshot of files changed by adding Application Insights

  • One new file is created:

    ConnectedService.json

    Screenshot of files changed by adding Application Insights

  • Three files are modified:

    appsettings.json

    Screenshot of files changed by adding Application Insights

    ContosoDotNetCore.csproj

    Screenshot of files changed by adding Application Insights

    Program.cs

    Screenshot of files changed by adding Application Insights

Synthetic transactions with PowerShell

Launching your app, and then clicking around on links manually can be used to generate test traffic. However, it is often helpful to create a simple synthetic transaction in PowerShell.

  1. Run your app by clicking IIS Express Screenshot of Visual Studio IIS Express icon

  2. Copy the url from the browser address bar. It is in the format http://localhost:{random port number}

    Screenshot of browser url address bar

  3. Run the following PowerShell loop to create 100 synthetic transactions against your test app. Modify the port number after localhost: to match the url you copied in the previous step.

    for ($i = 0 ; $i -lt 100; $i++)
    {
     Invoke-WebRequest -uri http://localhost:50984/
    }
    

Open Application Insights Portal

After running the PowerShell from the previous section, launch Application Insights to view the transactions and confirm that data is being collected.

From the Visual Studio menu, select Project > Application Insights > Open Application Insights Portal

Screenshot of Application Insights Overview

Note

In the example screenshot above Live Stream, Page View Load Time, and Failed Requests are currently not collected. The next section will walk through adding each. If you are already collecting Live Stream, and Page View Load Time, then only follow the steps for Failed Requests.

Collect Failed Requests, Live Stream, & Page View Load Time

Failed Requests

Technically Failed Requests are being collected, but none have occurred yet. To speed the process along a custom exception can be added to the existing project to simulate a real-world exception. If your app is still running in Visual Studio before proceeding Stop Debugging (Shift+F5)

  1. In Solution Explorer > expand Pages > About.cshtml > open About.cshtml.cs.

    Screenshot of Visual Studio Solution Explorer

  2. Add an Exception under Message= > save the change to the file.

    throw new Exception("Test Exception");
    

    Screenshot of exception code

Live Stream

To access the Live Stream functionality of Application Insights with ASP.NET Core update to the Microsoft.ApplicationInsights.AspNetCore 2.2.0 NuGet packages.

From Visual Studio, select Project > Manage NuGet Packages > Microsoft.ApplicationInsights.AspNetCore > Version 2.2.0 > Update.

Screenshot of NuGet Package Manager

Multiple confirmation prompts will occur, read and accept if you agree with the changes.

Page View Load Time

  1. In Visual Studio navigate to Solution Explorer > Pages > two files will need to be modified: _Layout.cshtml, and _ViewImports.cshtml

  2. In _ViewImports.cshtml, add:

    @using Microsoft.ApplicationInsights.AspNetCore
    @inject JavaScriptSnippet snippet
    

    Screenshot of code change to _ViewImports.cshtml

  3. In _Layout.cshtml add the line below before the </head> tag, but prior to any other scripts.

    @Html.Raw(snippet.FullScript)
    

    Screenshot of code change to layout.cshtml

Test Failed Requests, Page View Load Time, Live Stream

Now that you have completed the previous steps you can test out and confirm that everything is working.

  1. Run your app by clicking IIS Express Screenshot of Visual Studio IIS Express icon

  2. Navigate to the About page to trigger the test exception. (If you are running in Debug mode, you will need to click Continue in Visual Studio before the exception will be picked up by Application Insights.)

  3. Re-run the simulated PowerShell transaction script from earlier (You may need to adjust the port number in the script.)

  4. If the Applications Insights Overview is not still open, from Visual Studio menu select Project > Application Insights > Open Application Insights Portal.

    Tip

    If you aren't seeing your new traffic yet, check the Time range and click Refresh.

    Screenshot of Overview window

  5. Select Live Stream

    Screenshot of Live Metrics Stream

    (If your PowerShell script is still running you should see live metrics, if has stopped run the script again with Live Stream open.)

App Insights SDK Comparison

The Application Insights product group has been working hard to achieve as close to feature parity as possible between the full .NET Framework SDK and the .Net Core SDK. The 2.2.0 release of the ASP.NET Core SDK for Application Insights has largely closed the feature gap.

To understand more about the differences and tradeoffs between .NET and .NET Core.

SDK Comparison ASP.NET ASP.NET Core 2.1.0 ASP.NET Core 2.2.0
Live Metrics + - +
Server Telemetry Channel + - +
Adaptive Sampling + - +
SQL Dependency Calls + - +
Performance Counters* + - -

Performance Counters in this context refers to server-side performance counters like processor, memory, and disk utilization.

Open-source SDK

Read and contribute to the code

Video

Next steps