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:

[Serializable]
[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++)
        {
            a.MethodOne();
            a.MethodTwo();
        }
    }
}

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…

References: List executed code using PostSharp from our NCG partner Dror Helper at the Helper Code blog.

Related Whitepaper:

Best Practices for Secure Software Development

Best practices for all organizations that would like to produce more secure applications!

As part of the software development process, security professionals must make choices about where to invest their budget and staff resources to ensure that homegrown applications are as secure as possible. ESG research found organizations that are considered security leaders tend to make different choices than other firms.

Get it Now!  



.NET Code Geeks and all content copyright © 2010-2014, 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 two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.