Saturday, June 13, 2009

My baby steps to PostSharp 1.0

So… you downloaded PostSharp 1.0 and you installed it and are wondering… “What’s next?”.

Well my friends, let me walk you through the first steps of PostSharp. What could we do that would be simple enough? Hummm… what about writing to a debug window? That sounds simple enough! Let’s start. So I created a new Console Application project and I added the reference to PostSharp.Laos and PostSharp.Public. As a requirement, the class must be tagged with “Serializable” attribute and implement OnMethodBoundaryAspect (not in all case but let’s start small here).

Next, I have a few methods I can override. The two that we are interested in right now is “OnEnter” and “OnExit”. Inside of it, we’ll say which method we are entering and which one we are exiting. Here are my Guinea pig classes:

public class FooBar
{
    [DebugTracer]
    public void DoFoo()
    {
        Debug.WriteLine("Doing Foo");
    }

    [DebugTracer]
    public void DoBar()
    {
        Debug.WriteLine("Doing Bar");
    } 
}

[Serializable]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public class DebugTracer : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
        Debug.WriteLine(string.Format("Entering {0}", eventArgs.Method.Name));
    }

    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
        Debug.WriteLine(string.Format("Exiting {0}", eventArgs.Method.Name));
    }
}

See how simple this is? But… does it work? Let’s see the trace of calling each methods:

Entering DoFoo
Doing Foo
Exiting DoFoo
Entering DoBar
Doing Bar
Exiting DoBar

Isn’t that wonderful? Compile execute and enjoy. But… what about the community you say? Of course if the tool is not open source there is probably nothing built around it, right? Wrong!

Here is a few resources for PostSharp that include pre-made attributes that are ready to be used:

That was everything I could find. Do you know any others?

Submit this story to DotNetKicks

1 comment:

  1. Nice article! PostSharp is very cool. One of the best uses of PostSharp is to automate logging. But an unfortunate side-effect can be massive log files that are hard to read, hard to dig around in, and sometimes hard to just open without running out of memory.

    That problem is a thing of the past, thanks to a new PostSharp integration with Gibraltar. Gibraltar is a end-to-end logging solution. It integrates with PostSharp as well as many popular logging frameworks (log4net, NLog, etc -- and Trace, of course). And Gibraltar not only collects the data, it transmits and analyzes the data too.

    Check out the marriage of PostSharp and Gibraltar:
    http://www.gibraltarsoftware.com/See/PostSharp.aspx

    In full disclosure, I'm a tad biased because I'm one of the founders of Gibraltar and I wrote the PostSharp adapter. But, the fact is that PostSharp is the best .NET AOP framework around and Gibraltar works REALLY well with it.

    PostSharp works with Gibraltar to trace method execution, graph method execution time and log all exceptions (handled and unhandled) -- all without writing ANY additional code.

    ReplyDelete

Please try to keep comments relevant to the topic. All comments after 1 month will be moderated.