Monitoring non-web apps using Azure Application Insights (Part 1: Getting Started)
Tags: csharp, azure, devops, monitoring
Published: 2018-12-14 21:25:35

---
title: Monitoring non-web apps using Azure Application Insights (Part 1: Getting Started) 
published: true
description: This is the first post of a serie in which we will take a look at how we can use Application Insights to monitor desktop applications
tags: csharp,azure,devops,monitoring
cover_image: https://expecho.blob.core.windows.net/blogposts/appinsights-postbanner.png
series: Monitoring non-web apps using Azure Application Insights
---
This is the first post of a serie in which we will take a look at how we can use [Application Insights](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-overview) 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](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-analytics).
- It has many extension points. For example, you can [enrich or filter](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling) telemetry and [track your own](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics?toc=/azure/azure-monitor/toc.json) telemetry.
- You can [create and pin](https://docs.microsoft.com/en-us/azure/azure-monitor/visualizations?toc=/azure/azure-monitor/toc.json) your own visuals.
- It has powerful [alerting capabilities](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-overview).
- It is not restricted to workloads running in the cloud.
- It is [Open Source](https://github.com/Microsoft?utf8=%E2%9C%93&q=applicationinsights&type=&language=).

# 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](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-live-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](https://portal.azure.com/) and create an Application Insights resource:

![Create Resource](https://expecho.blob.core.windows.net/blogposts/create-appinsights.png)

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

![Get InstrumentationKety](https://expecho.blob.core.windows.net/blogposts/appinsights-getinstkey.png
)

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](https://www.nuget.org/packages/Microsoft.ApplicationInsights.WindowsServer/). (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
<?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:


```csharp
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](https://expecho.blob.core.windows.net/blogposts/appinsights-livestream.png )

![Live Metric Stream](https://expecho.blob.core.windows.net/blogposts/LiveMetricsStream-Basic.PNG
)

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.