Archive

Getting Started With PostSharp

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

1. Before your forget...

Bookmark us with del.icio.us and subscribe to our RSS feed.

2. Download & Install PostSharp

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

3. Open Your Favorite Project

Load one of your projects into Visual Studio 2005 or higher. Any project type will work, but for this example better choose a console application. No matter if you use C#, VB.NET, or another language: it will just work. (Since ASP.NET Web Sites don't use MSBuild, it's a little more difficult with these projects; refer to documentation for details.)

4. Add References To PostSharp

You want to use PostSharp in your project, isn't it? So add a reference to two assemblies: PostSharp.Public.dll and PostSharp.Laos.dll.

Add References to PostSharp

5. Create a New Aspect Class

Create a class and name it TraceAttribute.

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

6. Derive the Class from OnMethodBoundary

Includes the namespace PostSharp.Laos and make the new class inherit from OnMethodBoundaryAspect. Since this last class derives from System.Attribute, you are actually developing a custom attribute.

[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{

7. Implement the Handlers

Once the aspect custom attribute will be applied to a method, this method will invoke the aspect handlers. Do we want to trace methods? So just add trace logic to the handlers!

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

8. Apply the Custom Attribute to Methods

In your project, choose an existing class you want to log and decorate it with our new custom attributes. 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 a, say, very large assembly? You surely don't want to decorate every method to log, don't you? Well, no problem at all!

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

 

9. We're done! Compile & Execute

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

The output of our program with tracing.

10. How did it work?

Seems magic, huh? 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!

Build output: PostSharp is inserted in the build process.

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

More tutorials like this

This tutorial is a shorter version of a two-parts article published on codeproject.com. A good way to continue reading about PostSharp!

Comments are closed