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.

Attr2

attr3

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.

Attr5Attr4

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.

Attrib6

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

Happy monitoring.