Adding an OnException attribute using PostSharp

I am coming to AOP and it seems .NET PostSharp is the way to go.

I want to make some simple db entries when an exception occurs. However, it’s hard for me to find any real compelling 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 binding the [LogException] attribute to the method

But I get a compilation 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 admit that I am very new to this, but it seems like an interesting concept, I think I just need to point in the right direction

+4
source share
2 answers

I got it to work by extending OnExceptionAspect :

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

Original post:

He wants 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

+2
source

I used OnMethodBoundaryAspect instead of ExceptionHandlerAspect without any problems. And I have not done my sealed yet.

0
source

All Articles