Avoiding Code Pollution when using Application Insights SDK

“Insert a few lines of code in your application to find out what users are doing with it, or to help diagnose issues. You can send telemetry from device and desktop apps, web clients, and web servers. Use the Azure Application Insights core telemetry API to send custom events and metrics, and your own versions of standard telemetry. This API is the same API that the standard Application Insights data collectors use”  …… Copied from here.

The above statement is very true and my experience of using Application Insights for custom events has been great. It is extremely powerful and easy to use. The challenge comes when you have to instrument a large number of classes and methods throughout you code to enable telemetry collection. Although the SDK provides some great out-of-the-box functionality for tracking page load time, external dependencies (database/web service) and code exceptions just by including the SDK, there are lots of scenarios where you must add code and use custom events.

I worked on a simple pattern that can simplify this to some degree by using Custom Attributes a functionality that exists in .Net. I will illustrate my work here which is a very simple implementation of a Custom Attribute, that can be applied to any class or method in your solution, simply by scaffolding your class or method. The first code fragment below shows the Custom Attribute and the next code fragments show the usage and results.


As highlighted, this custom attribute can be targeted to classes or methods, a name and a user account type can be passed as a parameter. Sample usage is illustrated below.



In the two examples above the Custom Attribute is applied on the classes and a user level of “Admin” is passed on the first example and default user level “User” is accepted in the second.

In the code below the Custom Attribute is applied not to the class but to a method with a user level “Co-Admin”.


When we run this sample and instantiate these classes and methods we see the Application Insights telemetry information sent to the portal.


So we got 3 custom events with the user-id property what we set.

This illustrates the SDK use in its simplest form. In the next posts I will dive into some more use cases for Application Insights Custom Events and try to build some more Custom Attributes for tracking user response time between selections on a page, execution time of a method call inside your application, async api calls and .net core dependency calls etc.

Happy monitoring with Application Insights. Stay tuned.



One thought on “Avoiding Code Pollution when using Application Insights SDK

  1. Pingback: Using Application Insights SDK in Aspect-oriented programming (AOP) style with .NET. | Nasir's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s