Close

BLOGS

Adding Application Insights to your .Net Core project in Visual Studio

Application Insights is an extensible analytics platform that monitors the performance and usage of your live ASP.NET Core web applications.

With Asp.Net Core 2.0, ApplicationInsights is included by default when running from Visual Studio. You only need to configure the instrumentation key so that telemetry is sent to ApplicationInsights service. If instrumentation key is not added:

  • - ApplicationInsights will be enabled while debugging under Visual Studio 2017.
  • - ApplicationInsights telemetry will be shown in Visual Studio 2017.
  • - Nothing gets sent to the ApplicationInsights service, unless an instrumentation key is added.

You probably don't need to follow these manual steps below (except the instrumentation key section) unless you upgraded from a previous version.

Follow the steps below, skippings the ones as indicated:

  • - Create an Application Insights resource
  • - Add Application Insights NuGet package dependency to .csproj
  • - Add Application Insights instrumentation key
  • - Add Application Insights instrumentation in code
  • - Add Application Insights JavaScript instrumentation to the _ViewImports.cshtml, _Layout.cshtml

 

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 -> Monitoring + management -> Application Insights). Select the ASP.NET application type.
  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 .csproj

If the following package reference already exists in you .csproj, then there is no need to explicitly add ApplicationInsights package. This is a meta package and it contains the ApplicationInsights package.

<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />

If the above package reference is not present, then add the following to explicitly add ApplicationInsights.

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.0" />

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

As of now, the version of Microsoft.ApplicationInsights.AspNetCore included in Microsoft.AspNetCore.All (v2.0.0) meta package is 2.1.1. You can explicitly add a reference to Microsoft.ApplicationInsights.AspNetCore when you want a version higher than the one shipped with Microsoft.AspNetCore.All metapackage.

 

Add the instrumentation key.
Option 1: Use appsettings.json.

You can control whether telemetry is sent to ApplicationInsights service by setting/not setting instrumentation key. You may have separate appsettings.json for each environment, and each can contain different instrumentation keys, or do not contain anything at all if you do not want telemetry to be sent to ApplicationInsights service.

{
  "ApplicationInsights": {
    "InstrumentationKey": "11111111-2222-3333-4444-555555555555"
  }
}
Option 2:

Set instrumentation key into environment variable APPINSIGHTS_INSTRUMENTATIONKEY.

Both the above options work only if Application Insights is enabled is code with UseApplicationInsights() extension. As of now its not supported with AddApplicationInsightsTelemetry() as mentioned here

Option 3:

Add instrumentation key in code while enabling ApplicationInsights instrumentation. See the following section.

 

Add Application Insights instrumentation in code.

There are two options for instrumenting your code. These are intended to be mutually exclusive, only use one or the other and not both together!

Both options below allow passing an instrumentation key as a parameter to the call AddApplicationInsightsTelemetry() or UseApplicationInsights(). This is required if the key is not set via options mentioned at the beginning.

 

Option 1: Program.cs (Recommended)

The preferred default mechanism is to use the UseApplicationInsights extension method from the WebHostBuilder instance.

Starting with Asp.Net Core 2.0, all project templates include a call to WebHost.CreateDefaultBuilder() in Program.cs which automatically does everything needed to read settings from appsettings.json/environment variables and initialize configuration variables.

public static IWebHost BuildWebHost(string[] args) =>

    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build(); 

 

Modify the above code as follows to configure ApplicationInsights.

public static IWebHost BuildWebHost(string[] args) =>

    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

 

Option 2: Startup.cs
The older extension method AddApplicationInsightsTelemetry on the IServiceCollection is still available for customized configuration using one of its overloaded signatures.

In the method ConfigureServices of your Startup class, add the Application Insights service as follows:

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

This method has an override which can accept configuration as well.

var configBuilder = new ConfigurationBuilder()
                .SetBasePath(this.hostingEnvironment.ContentRootPath)
                .AddEnvironmentVariables();
var Configuration= configBuilder.Build();
services.AddApplicationInsightsTelemetry(Configuration);

 

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

(if present)

In _ViewImports.cshtml, add injection:

@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

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:

@Html.Raw(JavaScriptSnippet.FullScript)
</head>

 

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.
  • - Set alerts to notify you when your app goes off-limits.

  

Get more telemetry
  • - Monitor dependencies to see whether 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.

 

Print
Posted: Sep 23, 2018,
Comments: 0,
Author: Urish Arora
Rate this article:
5.0

Urish AroraUrish Arora

Other posts by Urish Arora

, Contact author

Please login or register to post comments.

Name:
Email:
Subject:
Message:
x

SEARCH

Categories

Subscribe to our blog

«November 2019»
MonTueWedThuFriSatSun
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

  • Featured Posts
  • Recent Posts
  • Recent Comments
Dynamics365Authority is a community platform for Professionals and Students to contribute or share their knowledge and skills on Microsoft Dynamics 365 Technologies. Our primary goal is to invite professionals on Dynamics 365 technology across the world to contribute & share their knowledge and skills through their blogs so that we can help other developers.
Stay updated with Dynamics 365 technology. Improve skills and knowledge from our blogs, articles and code snippets. Learn it. Follow professionals and learn from them.
Unified Service Desk Book(Online Edition) 

FOLLOW US

Stay connected with us on our social media channels for latest articles, blogs posts etc.  We will keep updating regularly on our social media platform and web platform.

FACEBOOK

Follow us on Facebook

 

TWITTER

View all our tweets

 

DYNAMICS365AUTH

Join us now

 

BLOGS

View our blogs

 

 

Latest Blog

Posted: Jun 20, 2019

Overview - Appendix (Unified Service Desk)

This blog is about Appendix

Read more
Posted: Jun 20, 2019

Overview - Troubleshoot and Debug (Unified Service Desk)

This blog is about Troubleshoot and Debug in Unified Service Desk

Read more
Posted: Jun 20, 2019

Overview - Performance (Unified Service Desk)

This blog is about the Performance in Unified Service Desk

Read more
RSS

ADDITIONAL RESOURCES

Dynamics 365 Authority.com - Search the library for "Dynamics 365"

  What's New in Dynamics 365

  Dynamics 365 Documentation

  

  Sign-up for 30 days trial

 

 

Recent comments

Ashish:

Nice blog. Very helpful to get all references to white papers at one place.

Previous Next

About Us

Here comes the Dynamics 365 Authority - Great technical blog posts are hidden gems. They are hard to find simply because not enough of us write them in the first place. Yet technical blogging is one of the best things we can do not only for ourselves, but also as members of the wider community of developers.

Dynamics 365 Authority leverages this platform to help developers and users to move from traditional paper-based process to modern digital business process. Dynamics365Authority.com helps your business grow, evolve and transform.

Dynamics365Authority.com is your Digital Transformation Partner

Recent Posts

Overview - Appendix (Unified Service Desk)

This blog is about Appendix
  • 244
  • Article rating: 5.0

Overview - Troubleshoot and Debug (Unified Service Desk)

This blog is about Troubleshoot and Debug in Unified Service Desk
  • 372
  • Article rating: 5.0
RSS

Get in touch

Follow Us

 

Tag cloud: Dynamics365Authority.com with Urish Arora; Dynamics365Authority.com; Sales; Leading community site on MS Dynamics 365; Dynamics 365 Authority; Service;Top CRM Blogs; Urish Arora - Australia's leading expert on Digital Transformation;  Digital Transformation; Field Service;Top Articles on MS Dynamics CRM Relationship Insights; Office 365 Security Compliance; Project Service; Microsoft Dynamics 365 for Sales; Relevance Search; Goal Metrics; Activities and Case; Sales Literature;Knowledge Management;

Terms Of UsePrivacy StatementCopyright 2019 by Dynamics365Authority
Back To Top