e-Zest members share technology ideas to foster digital transformation.

Microsoft’s Azure Application Insights for WPF Desktop Application

Written by Arpana Singh | Feb 6, 2018 5:44:00 AM

 

What is Application Insights?

Application Insights is an extensible Application Performance Management (APM) service for web developers who build and manage apps on multiple platforms. It is generally used to detect & diagnose issues and understand usage for your web apps and services. However, Application Insights was not officially adaptable for use with desktop applications.

With a few tweaks, we can make it work for your desktop applications. This blog article will help guide you through the steps required to implement Application Insights in your desktop application using WPF technology with success as well as help you track it using Visual Studio and Azure portal.

So let us start:

  • Create a simple MVVM WPF application.
  • Create a new Application Insights instance on the Azure portal, and get the instrumentation key.
  • Now set up Application Insights in your Visual Studio application.
  • In Visual Studio create or open your project then right click it in the Solution Explorer and choose Manage NuGet PackagesThis will open the NuGet package manager. Type Application Insights in the search box, and add these 2 NuGet packages to your application:
        Microsoft.ApplicationInsights

                  Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel

  • Click install and allow the NuGet package manager to make the required changes to your project.

Accept the Terms of Service and after that Application Insights will have been added to your project.

(Note – just for knowledge: The first package contains the TelemetryClient object. This is the object used to send data to Azure. The second package is not mandatory but strongly recommended. Indeed, the TelemetryClient store events in the memory and send them in batch at regular intervals. So, some events may get lost if the application crashes. Paradoxically, the exception is very important in this case. The ServerTelemetryChannel, provided by the second package, stores the events in a file, and then it sends them to Azure at regular intervals. So, if the application terminates without sending some events, they'll be sent the next time you start the application.)

  • Now create a new class in your WPF application and paste the following code in it. Paste your instrumentation key which we created in step 2 above, in below line of assignment to TelemetryKey variable:

public static class Telemetry
{
private const string TelemetryKey = "<INSERT YOUR INSTRUMENTATION KEY HERE>";

private static TelemetryClient _telemetry = GetAppInsightsClient();

public static bool Enabled { get; set; } = true;

private static TelemetryClient GetAppInsightsClient()
{
var config = new TelemetryConfiguration();
config.InstrumentationKey = TelemetryKey;
config.TelemetryChannel = new Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel();
//config.TelemetryChannel = new Microsoft.ApplicationInsights.Channel.InMemoryChannel(); // Default channel
config.TelemetryChannel.DeveloperMode = Debugger.IsAttached;
#if DEBUG
config.TelemetryChannel.DeveloperMode = true;
#endif
TelemetryClient client = new TelemetryClient(config);
client.Context.Component.Version = Assembly.GetEntryAssembly().GetName().Version.ToString();
client.Context.Session.Id = Guid.NewGuid().ToString();
client.Context.User.Id = (Environment.UserName + Environment.MachineName).GetHashCode().ToString();
client.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
return client;
}

public static void SetUser(string user)
{
_telemetry.Context.User.AuthenticatedUserId = user;
}

public static void TrackEvent(string key, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null)
{
if (Enabled)
{
_telemetry.TrackEvent(key, properties, metrics);
}
}

public static void TrackException(Exception ex)
{
if (ex != null && Enabled)
{
var telex = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(ex);
_telemetry.TrackException(telex);
Flush();
}
}

internal static void Flush()
{
_telemetry.Flush();
}
}

  • Now we can use it all over in our WPF application, wherever we want to track something, using code:

    Telemetry.TrackEvent("Woo hoo!!! Tracking my WPF application events on AI");
  • I have used it in my Execute Command class which is implementing ICommand interface.

    So, any event in my WPF application, which is using this command class to execute an event, will be logged in Application Insights. In the case any exception occurs it will also get logged.

  • Following is the code for complete implementation of command class.

There are two ways in which to track Application Insights.

Track telemetry data directly from Visual Studio

  • In tool bar - Click on “Application Insights”.

  • After clicking “Application Insights”, following screen will appear.

  • This screen will allow you to search and filter the trend or other telemetry data. In essence, this screen provides the same functionality as the portal. You can select the time range as per your choice, or you can also refine by Application Version, Event Name, Event time, Exception type, message, Problem Id, Operating System etc.
  • In addition, when you search for exceptions you can jump right to the code causing that exception. That provides you a great developer experience!

    Track telemetry data from Azure Portal

  • Click on “More Services”.
  • Go to “Application Insights” and select your resource group:

             

  • Now click on “Open Chart In Analytics” icon.

  • Now click on “Open Chart In Analytics” icon and go to Usage. There you can see the following screens in tabular and chart format from “Table” & “Chart” tab respectively.:

Summary

In conclusion, Application Insights is now supported also desktop applications.