List executed code using PostSharp

This post was created to answer a question by Laimonas Simutis on the ALT.NET mailing list – how to list all executed code
There where many good ideas and your truly suggested using PostSharp – mainly because this is one of the examples I use in my AOP and PostSharp presentation. And so without further ado here is my solution for your consideration:
The “tracing” attribute 
For this purpose I choose to use OnMethodBoundaryAspect which enable me to add hooks on method’s enter and exit:
[MulticastAttributeUsage(MulticastTargets.Method, AllowMultiple = true)]
public class TraceMethodCallsAttribute : OnMethodBoundaryAspect
    private string _methodName;

    public override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo)
        _methodName = method.DeclaringType.Name + "." + method.Name;

    public override void OnEntry(MethodExecutionArgs args)
        Console.WriteLine(">> {0}", _methodName);

    public override void OnExit(MethodExecutionArgs args)
        Console.WriteLine("<< {0}", _methodName);
This is your simple method boundary aspect which is good to add custom functionality before and after method call as well as on exception and/or successful completion. This time I only needed the enter and exit functionality.
My attribute has several attribute of it’s own, one simple for serialization and the other is where the magic happens – the MulticastAttributeUsage that tells PostSharp that this attribute should be automatically applied to every method in the class or assembly.
Other than that I’ve also used CompileTimeInitialize in order to save some cycles by “calculating” the method name only once (at compile time) instead of each time the method is called.
Using the attribute
Now all I have to do is add this line to the AsseblyInfo.cs file:
[assembly: TraceMethodCalls]
And that’s it – running the following program:
class Program
    static void Main(string[] args)
        var a = new A();
        for (int i = 0; i < 3; i++)

public class A
    public void MethodOne()
        Console.WriteLine("I am in method one");

    public void MethodTwo()
        Console.WriteLine("I am in method two");
Provides the desired result 
Happy coding…
Reference: List executed code using PostSharp from our NCG partner Dror Helper at the Helper Code blog.

Leave a Reply

Submit Comment

4 − = two

.NET Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on .NET Code Geeks are the property of their respective owners.
.NET is a trademark or registered trademark of Microsoft Corporation in the United States and other countries.
.NET Code Geeks is not connected to Microsoft Corporation and is not sponsored by Microsoft Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.