• 2 min read

Application Insights now supports ASP.NET 5 applications

We just released the first preview of Application Insights for the ASP.NET v5 (a.k.a ASP.NET vNext) web applications and will allow you to monitor server requests and exceptions for your application as well as collect end user information and browser page performance.

We just released the very first preview of Application Insights for the ASP.NET 5 web applications. You can find the NuGet on nuget.org and will allow you to monitor server requests and exceptions for your application as well as collect end user information and browser page performance.

Please follow this getting started guide to onboard your ASP.NET 5 application to Application Insights.

ASP.NET application model was changed drastically with the new generation of ASP.NET. One of the big changes is dependency injection that became a first class citizen. This changed the way you interact with telemetry SDK in ASP.NET 5 web applications.

As you may notice, Application Insights SDK for ASP.NET 5 applications takes dependency on the same “basic API” NuGet that is used for the classic ASP.NET application monitoring today. It uses the same telemetry types, data contracts and channel however, there is one important difference – for ASP.NET 5 applications singleton TelemetryConfiguration.Active will not be initialized – you’ll need to use dependency injection instead.

This article describes how to use Application Insights SDK for classic ASP.NET applications today. Code you’ll typically write looks like this:

var tc = new TelemetryClient(); 
tc.TrackEvent("Order complete");

This code assumes some magic. Specifically that TelemetryClient will be initialized with the proper Instrumentation Key so it will know where to send event. Technically it is equivalent to this code when global singleton TelemetryConfiguration.Active passed to constructor:

var tc = new TelemetryClient(TelemetryConfiguration.Active);
tc.TrackEvent("Order complete");

This code will NOT work with ASP.NET 5 Application Insights SDK. The very nature of ASP.NET 5 is to avoid singletons and use dependency injection instead. This wiki page explains how you can get TelemetryClient class using dependency injection to track your events.

First, you’ll need to get an instance of TelemetryClient. Suppose you need to track event from your home controller. You’ll need to create a constructor that accepts TelemetryClient as an argument:

public class HomeController : Controller 
{ 
    private TelemetryClient telemetry;
    public HomeController(TelemetryClient telemetry) 
    {
        this.telemetry = telemetry;
    }

Now you can use this telemetry client to track events and other custom telemetry:

    public IActionResult Index() 
    {
        this.telemetry.TrackEvent("HomePageRequested"); 
        return View(); 
    }

As all the source code and history of this SDK development is available on GitHub you may find how implementation of SDK evolved over time moving from singleton-based approach to the one based on dependency injection. We will be happy to hear your ideas, and feedback. Just file an issue and tell us what you think.