Using Application Insights SDK in Aspect-oriented programming (AOP) style with .NET.

In my previous post I talked about using Custom Attributes to avoid repetition, code pollution etc., and illustrated it from a very high level to use with Application Insights API, custom events.

C# – Attributes. Advertisements. An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute

The problem I am trying to solve here is to separate the Application Insights SDK code, hide it from the developer and simplify adding the functionality to Classes, Methods and other dependencies that need to be tracked for performance, logging and auditing. I ran my thought process with my friends and fellow workers Alex and Sujit, got some great advise from both to look further into Visual Studio Profiler and Aspects based development, AOP (Aspect-oriented programming) I also looked into PostSharp a popular framework for AOP. I tried out few scenarios and all my thoughts started to come together, VERY COOL. During my googling (wait.. binging!!) I educated myself more and more on AOP (very interesting) I found many articles and landed on this, it gave me great ideas on using pure .NET (RealProxy Class) to implement a solution. Digging more into RealProxy and attributing and going through many posts and techniques certainly helped. Making the long story short I was able to assemble my approach that can be used for Profiling methods and classes for performance, logging and auditing using the AOP and attributing approach, that can simplify injecting Application Insights API into any application. I am sure it can be greatly enhanced per implementations but this can definitely get you started.

The pattern is simple, here are the steps:

Create your Custom AttributeAttr-1
The next step is to build the Proxy for this Custom attribute, which is derived from the RealProxy class in .NET framework. The RealProxy class provides transparent proxy for the object. The transparent proxy provides the illusion that the actual object resides in the client’s space.

To support the performance tracking I implemented a stopwatch and a Write Telemetry method, the stopwatch kicks at at the beginning of the Invoke method carrying our Custom Attribute, and at the end, Telemetry is written with the elapsed time.



This blog article explains in detail the use of RealProxy class for AOP and provides samples for implementing your own proxy classes derived from RealProxy.

The next step is to put the new Custom Attribute on the methods on the Class that need to be tracked for Performance. It is that simple but you do have to build Interfaces for your Classes a good OOP practice anyway. This is illustrated below.


Now when the Add, Update and Delete are called the Performance can be recorded by the Application Insights Custom Events. Our new Custom Attributes can be applied to any number of methods.

This concludes this post, a great use of this pattern can also build a centralized logging framework and I will illustrate that in the next post.


3 seconds for update, 5 seconds for delete and 7 seconds for add as we expected based on the demo delay parameters.

Happy monitoring.




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.