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
- 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:
If it's a new project...
Make sure Application Insights is selected when you create a new project in Visual Studio.
... or if it's an existing project
Right-click the project in Solution Explorer, and choose Add Application Insights Telemetry or Configure Application Insights.
- ASP.NET Core project? - Follow these instructions to fix a few lines of code.
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.
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:
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.
... 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.
- 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:
Click any tile or chart to see more detail.
More detail in the portal
Live Metrics Stream displays telemetry almost instantly.
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.
- 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.
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.
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.
Application Insights sends telemetry from your app to the Application Insights portal (which is hosted in Microsoft Azure):
So the command did three things:
- 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.
- 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.
- Inserts the instrumentation key in
ApplicationInsights.config, so that the SDK can send telemetry to the portal.
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
- 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.)
Got a problem? If you want to insert code in your app to help diagnose it, you have several options:
- Capture log traces: If you're already using Log4N, NLog, or System.Diagnostics.Trace to log trace events, then the output can be sent to Application Insights so that you can correlate it with requests, search through it and analyze it.
- Custom events and metrics: Use TrackEvent() and TrackMetric() either in server or web page code.
- Tag telemetry with additional properties
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.
Version and release tracking
Track application version
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.
If you use Visual Studio Team Services, you can get an annotation marker added to your charts whenever you release a new version.
|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.