Beyond Hello World: PostSharp with Gibraltar Software and SmartInspect

by Gael Fraiteur on 26 Nov 2009

Tracing has long been the "Hello, world" sample of Aspect-Oriented Programming. PostSharp does not make exception: tracing is still the first aspect visible on the home page, and most of early PostSharp bloggers did demonstrate AOP on a tracing aspect.

Yet, as tracing is a fundamental part of any non-trivial application, some advanced tools are available to .NET developers. Two of them deserve a special attention since their vendor provides aspects that can be directly used with PostSharp: Gibraltar and SmartInspect.

Don't ask me to compare these toolkits: they are actually sharp competitors. Both have good web sites and a free trial, so I can only recommend you give them both a try! And both have identified PostSharp as a natural complement to their product (Gibraltar being the first).

Gibraltar

I have a special moral debt to Jay Cincotta, one of the guys behind Gibraltar, since he recorded an amazing webcast of using PostSharp and Gibraltar together. Jay, thank you very much for this video, I know how hard it is to produce!

With these aspects you can declaratively add Gibraltar logging to your programs by simply tagging whatever methods, classes or namespaces you wish. This lets you add powerful logging to your applications without the effort, complexity or clutter of writing procedural logging code.

  • [GTrace] logs entry and exit from tagged methods including appropriate message indentation.
  • [GTraceField] logs every change in value of a tagged field.
  • [GException] logs exceptions at the point they are raised. This is a handy safe guard to ensure that both handled and unhandled exceptions are logged by Gibraltar.
  • [GTimer] lets you graph method execution time in Gibraltar providing invaluable information to identify bottlenecks and optimize performance.

These aspects (not the toolkit itself) are open-source and are hosted on Google Code at https://code.google.com/p/postsharp-user-plugins/.

For more information, see Using PostSharp with Gibraltar on their web site.

SmartInspect

Following Gibraltar, SmartInspect provides similar aspects:

  • [SiTrace] automatically generates two log messages for each method call: one for entering a method and one when a method exits. You can also log the passed method arguments (when entering a method) and the method result (when exiting a method), respectively.
  • [SiException] records all exceptions occurring in methods enhanced by the aspect.
  • [SiField] lets you automatically record any changes to values of object or class fields.

The aspects can be downloaded from SmartInspect Resources & Extras (look for SmartInspect PostSharp Adapter).

For more information, see Aspect-oriented logging for .NET with PostSharp and SmartInspect on their web site.

Support for Other Logging Frameworks

If you're interested in using PostSharp with Log4Net, remember to check out Log4PostSharp, a project of Michal Dabrowski and Mark Kharitonov. Note that, contrarily to what's now offered by Gibraltar and SmartInspect, Log4PostSharp directly emits optimal instructions, resulting in much better runtime performance. This is not a feature of the logging framework but of the aspect implementation.

Interestingly, there is also some aging project from Eclipse, Build to Manage for .NET, providing PostSharp aspects for the IBM Tivoli platform.

PostSharp Emerged as the De-Facto Standard

What's happening there is that PostSharp is emerging as the de-facto standard for aspect-oriented programming on Microsoft .NET, just like AspectJ is for Java.

PostSharp 2.0 has been designed for the multi-vendor scenario, so that multiple aspects shall merge robustly when applied on the same element of code, even if these aspects come from different vendors who don't know about each other.

This means that ISVs can confidently rely on PostSharp to deliver aspects to their own customers. Good to know: the upcoming price structure shall make it very affordable for small ISVs to redistribute PostSharp free of royalties -- while enabling their own customers to use all professional features of PostSharp 2.0, including the IDE extension, in conjunction with their software.

Happy PostSharping!

-gael