It is not easy to monitor how our code behaves on a vast array of different machines. A myriad of different configurations can lead to errors that are difficult to reproduce and even more difficult to anticipate. And when the customer calls complaining about a crash, provided information on what lead to the problem is often incomplete or misleading. Fortunately, remote telemetry of software applications is here to help and is going mainstream even in the desktop area. Let’s see how easy it is to monitor a desktop Windows application using the new Azure Application Insights service: this article on the Azure site explains all the necessary steps. Summing up, here is what we need to do:
- create an Application Insights resource in the Azure portal
- make a copy of the Instrumentation key, as we will need it later in our app
- add one of the following NuGet packages: Microsoft.ApplicationInsights.WindowsServer for the full set of functionalities, including performance counter collection and dependency monitoring, or Microsoft.ApplicationInsights that includes the core API only
- initialize the TelemetryClient object in your app
- set the intrumentation key in the code of the app:
TelemetryConfiguration.Active.InstrumentationKey = " your key ";
- insert telemetry calls, like TrackPageView, TrackException etc.
For additional reference on these steps, check out the official ApplicationInsights repository on GitHub.
However, before peppering our code with calls to the TelemetryClient like the following ones
- TrackPageView(pageName) on switching forms, pages, or tabs
- TrackEvent(eventName) for other user actions
- TrackMetric(name, value) to send regular reports of metrics not attached to specific events
- TrackTrace(logEvent) for diagnostic logging
- TrackException(exception) in catch clauses
let’s try to adapt the logging interface we already have in place to also support telemetry data (TrackTrace and TrackException will likely match the Info and Error log levels) and create a new implementation of the logging interface that forwards data to both local log and remote telemetry at the same time with minimal effort. Once the benefits of telemetry are tangible, adding monitoring of users’ actions with TrackPage and TrackEvent will be easy to justify.
Once the telemetry integration is done and the software is deployed, we log into the Azure dashboard and open Application Insights for the test app: