loading...
Cover image for Monitoring non-web apps using Azure Application Insights (Part 1: Getting Started)

Monitoring non-web apps using Azure Application Insights (Part 1: Getting Started)

expecho profile image Peter Bons Updated on ・4 min read

This is the first post of a serie in which we will take a look at how we can use Application Insights to monitor other applications besides web applications. Most application landscapes are a mixture of cloudbased and on-premises applications. Some are webbased, some are desktop applications. Would it not be great if we can use one tool to monitor them all? Luckely there is Application Insights, a low cost monitoring solution running on Azure.

This post will cover the introduction of Application Insights and the first steps of monitoring a desktop application.

What is Application Insights?

Application Insights is an extensible Application Performance Management (APM) service for web developers on multiple platforms. Use it to monitor your live web application. It will automatically detect performance anomalies. It includes powerful analytics tools to help you diagnose issues and to understand what users actually do with your app. It's designed to help you continuously improve performance and usability. It works for apps on a wide variety of platforms including .NET, Node.js and J2EE, hosted on-premises or in the cloud. It integrates with your DevOps process, and has connection points to a variety of development tools.

There are a couple of things to like about Application Insights:

  • It is tightly integrated in many Azure Services like Azure Web Apps, Azure Functions, Azure Kubernetes Services and many more.
  • It is really easy to configure. (Very often it is either automatically done or it need at most one or two clicks)
  • It has a powerful querying mechanism called Application Insights Analytics.
  • It has many extension points. For example, you can enrich or filter telemetry and track your own telemetry.
  • You can create and pin your own visuals.
  • It has powerful alerting capabilities.
  • It is not restricted to workloads running in the cloud.
  • It is Open Source.

Monitoring a simple desktop application

Most documentation and blogposts regarding Application Insights deal with setting up monitoring web applications. This is really straight forward since all that needs to be done is adding some NuGet packages, provide an Instrumentation Key and add some config code. Then you get automatically request, exception and trace telemetry send to Application Insights since it can hook into several extension points provided by the Asp.Net (Core) framework. But unfortunately, when you are stuck developing desktop applications using WPF or WinForms for example, there is no such mechanism as a request pipeline to use. But, as we will explore in this serie, that does not mean we cannot use Application Insights at all.

So, without further ado, this is what will be covered by this serie:

  1. Provisioning an Application Insights resource in Azure and enable the Live Metrics Stream of our little console application.
  2. Add basic instrumentation to our code and have it send to Application Insights.
  3. Enhancing our instrumentation by automatically tracking exceptions, requests and dependencies.

Provisioning an Application Insights resource

The first thing we need to do is obviously creating an Application Insights resource for us to use. Then we need to extract the Instrumentation Key that we need in our application to configure the Application Insights integration.

Go to the Azure Portal and create an Application Insights resource:

Create Resource

Once the resource is created, browse to it and note the instrumentation key. We will use this key in our application.

Get InstrumentationKety

That is it for now. It is time to create our application.

Enabling Application Insights in our application

I created a .Net 4.7.2 console application using Visual Studio 2017. To use Application Insights we will have to add a reference to the Microsoft.ApplicationInsights.WindowsServer NuGet Package. (This will automatically reference the Microsoft.ApplicationInsights package)

When the NuGet Packages are installed a new file is added to the project, called ApplicationInsights.config. Open that file and add the InstrumentationKey add the top like this:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <InstrumentationKey>YOUR_KEY_HERE</InstrumentationKey>
  <TelemetryInitializers>
    <Add Type="Microsoft.ApplicationInsights.DependencyCollector.HttpDependenciesParsingTelemetryInitializer, Microsoft.AI.DependencyCollector"/>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer"/>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureWebAppRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer"/>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer"/>
  </TelemetryInitializers>
  <TelemetryModules>
...

Now, we could have chosen to just send some "Hello World" message from our application to Application Insights but let us just make it a bit more juicier than that. Would it not be nice if we have some real-time insights of the cpu and memory usage of our machine running the application? Let us try by modifying our console application to make this happen:

using System;
using Microsoft.ApplicationInsights.Extensibility;

namespace NonWebIntegrationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            _ = TelemetryConfiguration.Active;

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

The line _ = TelemetryConfiguration.Active; triggers the SDK to start sending telemetry to Application Insights.

Now, build and execute the application and leave it running. Browse to the Azure Portal and navigate to your Application Insights resource. From there, open the Live Metrics Stream tab and observe the live feed of the application:

Live Metric Stream-Nav

Live Metric Stream

It might take a few moments before the live stream shows data, so be patient.

That is it for now! We have created an Application Insights resource and configured it successfully in our console application. This will be our starting point for the next post, stay tuned.

Posted on by:

expecho profile

Peter Bons

@expecho

Passioned C# / .Net Developer / Architect with a strong focus on architecture, backend & Azure Cloud development. Love to learn & share my knowledge using StackOverflow, GitHub and knowledge sessions.

Discussion

pic
Editor guide
 

looking forward part II

 

Well, it took a while due to circumstances but here we go: dev.to/expecho/monitoring-non-web-...