views:

705

answers:

2

I am adventuring into some AOP and it seems with .NET PostSharp is the way to go.

I want to do some simple logging to the db when an exception occurs. However I am finding it difficult to find any real solid examples of using PostSharp beyond the basics. I tried the following:

[Serializable]
public sealed class LogExceptionAttribute : ExceptionHandlerAspect
{
    public override void OnException(MethodExecutionEventArgs eventArgs)
    {
        //do some logging here
    }
}

And then attaching a [LogException] attribute to a method

But I get a compile error:

Error   7 The type "CoDrivrBeta1.Classes.LogExceptionAttribute" or one of its ancestor should be decorated by an instance of MulticastAttributeUsageAttribute. C:\work\CoDrivrBeta1\CoDrivrBeta1\EXEC CoDrivrBeta1

I have to confess I am very new to this, but it seems like an interesting concept, I think i just need to be pointed in the right direction

A: 

I've used OnMethodBoundaryAspect instead of ExceptionHandlerAspect without problems. And I've not made mine sealed either.

Joel Lucsy
+2  A: 

I got it to work by extending the OnExceptionAspect:

[Serializable]
public sealed class LogExceptionAttribute : OnExceptionAspect
{
    public override void OnException(MethodExecutionEventArgs eventArgs)
    {
        //do some logging here
    }
}


Original Post:

It wants you to add the Multicast attribute:

[Serializable]
[MulticastAttributeUsage(... Add Appropriate MulticastTargets ...)]
public sealed class LogExceptionAttribute : ExceptionHandlerAspect
{
    public override void OnException(MethodExecutionEventArgs eventArgs)
    {
        //do some logging here
    }
}

See this link for more details: http://doc.postsharp.org/1.0/UserGuide/Laos/Multicasting/Overview.html

Ryan Cook