Getting Started with PostSharp

PostSharp makes aspect-oriented programming easy. You can start writing your own aspect in a matter of minutes. Just follow the guide.

1. Download and install PostSharp

Yes, you have to download and install. But don't worry, it's only 5 MB. Execute the setup program (you will need administrative rights) and restart Visual Studio.

If you don't want to or cannot install software on your machine, you can download the zip package instead of the setup program, but then you have to enable PostSharp manually for your projects and the Visual Studio extension will not work. We suggest that for your first experience with PostSharp, you use the setup program.

2. Open your favorite project

Load one of your projects into Visual Studio 2008 or higher. Any project type will work, but for this example it's best to choose a console application. It doesn't matter whether you use C#, VB.NET, or another language: it will just work.

Since ASP.NET Web Sites do not use MSBuild, it's a little more difficult with these projects; refer to the documentation for details.

You can use PostSharp with Visual Studio 2005, but IDE enhancements will not be available.

3. Add a reference to PostSharp.dll

You want to use PostSharp in your project, don't you? So add a reference to a single assembly: PostSharp.dll.

4. Create a new custom attribute

Create a class and name it TraceAttribute; design the class just as if it were a custom attribute.

public sealed class TraceAttribute : Attribute { private readonly string category; public TraceAttribute(string category) { this.category = category; } public string Category { get { return category; } } }

5. Derive the class from OnMethodBoundaryAspect

Okay, you are not developing an ordinary custom attribute but an aspect. So include the namespace PostSharp.Aspects and makes the class inherit from OnMethodBoundaryAspect (which, itself, derives from System.Attribute).

using PostSharp.Aspects; [Serializable] public sealed class TraceAttribute : OnMethodBoundaryAspect

6. Implement the advices

Handlers are called advices in aspect-oriented programming. If we want to trace whenever a method enters or exits, we need to override the following methods:

public override void OnEntry(MethodExecutionArgs args) { Trace.WriteLine(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name), this.category); } public override void OnExit(MethodExecutionArgs args) { Trace.WriteLine(string.Format("Leaving {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name), this.category); }

7. Apply aspect to methods

In your project, choose an existing class you want to log and decorate it with our aspect– remember it is a custom attribute. All methods will be traced. If you prefer to trace some of these methods, don't apply the custom attribute on the class, but on each method.

internal static class Program { private static void Main() { Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); SayHello(); SayGoodBye(); } [Trace("MyCategory")] private static void SayHello() { Console.WriteLine("Hello, world."); } [Trace("MyCategory")] private static void SayGoodBye() { Console.WriteLine("Good bye, world."); } }

And what if you have, say, a very large assembly? You surely don't want to decorate every method to log, do you? Well, no problem.

[assembly: Trace("MyCategory", AttributeTargetTypes = "My.BusinessLayer.*")]

8. We're done! Compile & execute!

That's all, really! If you have a console application, the output should include tracing information.

9. How did it work?

Seems like magic, right? Well, to some extent, it is. Open the Output Window of Visual Studio and look at the Build Output: you will see that PostSharp has been invoked. This is the magic wand.

And now, inspect your assembly using .NET Reflector. You will see how PostSharp enhanced your code: