Set up Application Insights for ASP.NET

Azure Application Insights monitors your live application to help you detect and diagnose performance issues and exceptions, and discover how your app is used. It works for apps that are hosted on your own on-premises IIS servers or on cloud VMs, as well as Azure web apps.

Before you start

You need:

  • Visual Studio 2013 update 3 or later. Later is better.
  • A subscription to Microsoft Azure. If your team or organization has an Azure subscription, the owner can add you to it, using your Microsoft account.

There are alternative articles to look at if you are interested in:

1. Add Application Insights SDK

If it's a new project...

Make sure Application Insights is selected when you create a new project in Visual Studio.

Create an ASP.NET project

... or if it's an existing project

Right-click the project in Solution Explorer, and choose Add Application Insights Telemetry or Configure Application Insights.

Choose Add Application Insights

2. Run your app

Run your application with F5 and try it out: open different pages to generate some telemetry.

In Visual Studio, you see a count of the events that have been logged.

In Visual Studio, the Application Insights button shows during debugging.

3. See your telemetry...

... in Visual Studio

Open the Application Insights window in Visual Studio: Either click the Application Insights button, or right-click your project in Solution Explorer:

In Visual Studio, the Application Insights button shows during debugging.

This view ('Data from debug session') shows telemetry generated in the server side of your app. Experiment with the filters, and click any event to see more detail.

  • No data? Make sure the time range is correct, and click the Search icon.

Learn more about Application Insights tools in Visual Studio.

... in the portal

Unless you chose Install SDK only, you can also see the telemetry at the Application Insights web portal.

The portal has more charts, analytic tools, and dashboards than Visual Studio.

Open your Application Insights resource - either sign in to the Azure portal and find it there, or right-click the project in Visual Studio and let it take you there.

Right-click your project and open the Azure portal

  • Access error? If you have more than one set of Microsoft credentials, you might be signed in with the wrong set. In the portal, sign out and sign in again.

The portal opens on a view of the telemetry from your app: Application Insights Overview page

Click any tile or chart to see more detail.

More detail in the portal

  • Live Metrics Stream displays telemetry almost instantly.

    From the Overview blade, click Live Stream

    Open Live Stream at the same time as your app is running, to allow them to connect.

    Live Stream only shows telemetry for a minute after it is sent. For more historical investigations, use Search, Metrics Explorer, and Analytics. Data may take a few minutes to appear in these places.

  • Search shows individual events such as requests, exceptions, and page views. You can filter by event type, term match, and property values. Click any event to see its properties and related events.

    From the Overview blade, click Search

    • In development mode, you may see a lot of dependency (AJAX) events. These are synchronizations between the browser and the server emulator. To hide them, click the Dependency filter.
  • Aggregated metrics such as request and failure rates appear in the charts. Click any chart to open a blade with more detail. Click the Edit tag of any chart to set filters, size, and so on.

    From the Overview blade, click any chart

Learn more about using Application Insights in the Azure portal.

4. Publish your app

Publish your app to your IIS server or to Azure. Watch Live Metrics Stream to make sure everything is running smoothly.

You'll see your telemetry building up in the Application Insights portal, where you can monitor metrics, search your telemetry, and set up dashboards. You can also use the powerful Analytics query language to analyze usage and performance or find specific events.

You can also continue to analyze your telemetry in Visual Studio with tools such as diagnostic search and Trends.

Note

If your app sends enough telemetry to approach the throttling limits, automatic sampling switches on. Sampling reduces the quantity of telemetry sent from your app, while preserving correlated data for diagnostic purposes.

What did 'Add Application Insights' do?

Application Insights sends telemetry from your app to the Application Insights portal (which is hosted in Microsoft Azure):

So the command did three things:

  1. Add the Application Insights Web SDK NuGet package to your project. To see it in Visual Studio, right-click your project and choose Manage NuGet Packages.
  2. Create an Application Insights resource in the Azure portal. This is where you'll see your data. It retrieves the instrumentation key, which identifies the resource.
  3. Inserts the instrumentation key in ApplicationInsights.config, so that the SDK can send telemetry to the portal.

If you want, you can do these steps manually for ASP.NET 4 or ASP.NET Core.

To upgrade to future SDK versions

To upgrade to a new release of the SDK, open NuGet package manager again, and filter on installed packages. Select Microsoft.ApplicationInsights.Web and choose Upgrade.

If you made any customizations to ApplicationInsights.config, save a copy of it before you upgrade, and afterwards merge your changes into the new version.

Add more telemetry

Web pages and single-page apps

  1. Add the JavaScript snippet to your web pages to light up the Browser and Usage blades with data about page views, load times, browser exceptions, AJAX call performance, user and session counts.
  2. Code custom events to count, time, or measure user actions.

Dependencies, exceptions and performance counters

Install Status Monitor on each of your server machines, to get additional telemetry about your app. This is what you get:

  • Performance counters - CPU, memory, disk and other performance counters relating to your app.
  • Exceptions - more detailed telemetry for some exceptions.
  • Dependencies - calls to REST API or SQL services. Find out whether slow responses by external components are causing performance issues in your app. (If your app runs on .NET 4.6, you don't need Status Monitor to get this telemetry.)

Diagnostic code

Got a problem? If you want to insert code in your app to help diagnose it, you have several options:

Use Search to find and correlate specific events, and Analytics to perform more powerful queries.

Alerts

Be the first to know if your app has problems. (Don't wait until your users tell you!)

  • Create web tests to make sure your site is visible on the web.
  • Proactive diagnostics run automatically (if your app has a certain minimum amount of traffic). You don't have to do anything to set them up. They tell you if your app has an unusual rate of failed requests.
  • Set metric alerts to warn you if a metric crosses a threshold. You can set them on custom metrics that you code into your app.

By default, alert notifications are sent to the owner of the Azure subscription.

alert email sample

Version and release tracking

Track application version

Make sure buildinfo.config is generated by your MSBuild process. In your .csproj file, add:


    <PropertyGroup>
      <GenerateBuildInfoConfigFile>true</GenerateBuildInfoConfigFile>    <IncludeServerNameInBuildInfo>true</IncludeServerNameInBuildInfo>
    </PropertyGroup>

When it has the build info, the Application Insights web module automatically adds Application version as a property to every item of telemetry. That allows you to filter by version when performing diagnostic searches or when exploring metrics.

However, notice that the build version number is generated only by MS Build, not by the developer build in Visual Studio.

Release annotations

If you use Visual Studio Team Services, you can get an annotation marker added to your charts whenever you release a new version.

release annotation sample

Next steps

Working with Application Insights in Visual Studio
Debugging with telemetry, diagnostic search, drill through to code.
Working with the Application Insights portal
Dashboards, powerful diagnostic and analytic tools, alerts, a live dependency map of your application, and telemetry export.
Add more data
Monitor usage, availability, dependencies, exceptions. Integrate traces from logging frameworks. Write custom telemetry.