Photo by NASA on Unsplash

Azure Application Insights for web and desktop

Stephan Bester
5 min readMar 3, 2022

--

Application Insights is a feature of Azure that provides performance monitoring, trace logging and even Google Analytics-style usage tracking for your applications. It is part of the Azure Monitor service, Microsoft’s solution for observability of your cloud ecosystem.

In this article, I will describe the steps for introducing Application Insights into both web and Windows desktop applications.

Creating the resource

Insight data is gathered into an Application Insights instance in an Azure account. You can create this resource by going to the Azure Portal and choosing the Application Insights service.

Icon in Azure

Click Create and choose a subscription, resource group, name, region, etc. Once the resource is created, navigate to its overview blade and take note of the following properties:

  • Instrumentation Key
  • Connection String

The instrumentation key is a GUID that identifies the Application Insights resource to your application. The connection string is a setting that includes the instrumentation key as well as the ingestion endpoint. It has the advantage that it can be extended by Microsoft without affecting the schema of the application config file.

Configuring the application

In web applications

Telemetry can be added to e.g. an MVC project simply by right-clicking the project in Visual Studio and choosing Add -> Application Insights Telemetry.

Adding Application Insights Telemetry

This brings up a dialogue prompting you to choose a dependency. If you’ve already created the instance in Azure, choosing the “Azure Application Insights” option will also help you set up your project to connect to that resource.

Configure service dependency

Assuming your account in Visual Studio is linked to Azure, this will bring up a list of Application Insights instances in your subscription. Click Next and you will notice that the connection string value has been prepopulated. Click Next again and you will be presented with a summary of changes that will be made to your project, e.g.

Summary of changes

As the summary suggests, this imports several NuGet packages into your project, as well as adding a section (called ApplicationInsights) to your appsettings.json file which contains the connection string for your Applications Insights instance. The instrumentation key is included herein.

In an older .NET project this adds a new file “ApplicationInsights.config” instead. Warning: When I did this on a .NET Framework 4.7.2 MVC project, the “Application Insights Telemetry” option wouldn’t appear in the Add menu until after I ran the application once.

Once these changes have been applied, if you run your application, you can view telemetry data in Azure. For example, going to the “Live metrics” blade on your instance will show metrics for the last 60 seconds.

Live metrics in Application Insights

You can also search and filter traces and other events on the transaction search blade, view exceptions on the failures blade, and so on.

In desktop applications

The process for setting up Application Insights in classic desktop applications is somewhat more cumbersome. First, you need to install the following NuGet package:

This should install several dependent packages as well. The full list might be something like this:

Microsoft.ApplicationInsights
Microsoft.ApplicationInsights.Agent.Intercept
Microsoft.ApplicationInsights.DependencyCollector
Microsoft.ApplicationInsights.PerfCounterCollector
Microsoft.ApplicationInsights.WindowsServer
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel

This also creates an ApplicationInsights.config file and adds it to your project. However, at this point, the application doesn’t know where to send its data yet. You can add this to the aforementioned config file by including the connection string, e.g.

<ApplicationInsights>
...
<ConnectionString>InstrumentationKey=your-key;IngestionEndpoint=https://southafricanorth-1.in.applicationinsights.azure.com/</ConnectionString>
...
</ApplicationInsights>

If you use this approach, you need to initialize Application Insights by creating an instance of TelemetryClient. You don’t need to keep a reference to this instance for the connection to work. (And before you ask — yes, constructors with side effects like this are to be considered bad practice.)

new TelemetryClient();

Alternatively, you can set the instrumentation key in code by adding a line like the following somewhere in the start-up section of your application:

TelemetryConfiguration.Active.InstrumentationKey = "your-key";

Doing this seems to initialize Application Insights as well so you don’t need to use the ugly TelemetryClient trick above.

With log4net

If your application is using log4net, you can easily direct your existing log messages to Application Insights. This allows for centralized logging and can be useful for making your application cloud-ready.

You can enable this by adding the following NuGet package to your project:

Then configure your log4net appender(s) to use the following type:

Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender

Warning: For this to work in desktop applications, you need to ensure that the Application Insights client is being initialized in code (see the desktop section above).

Further reading

Used for this article

  • .NET 5.0 (web applications)
  • .NET Framework 4.7.2 (desktop applications)
  • Visual Studio Professional 2019

--

--

Stephan Bester

Software developer walking the edge between legacy systems and modern technology. I also make music: stephanbmusic.com