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:Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
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.)
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();
}
}
Summary
In conclusion, Application Insights is now supported also desktop applications.