4

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

4

2 に答える 2

2

を拡張することで機能するようになりましたOnExceptionAspect

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

元の投稿:

マルチキャスト属性を追加する必要があります。

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

詳細については、次のリンクを参照してください: http://doc.postsharp.org/1.0/UserGuide/Laos/Multicasting/Overview.html

于 2008-12-09T19:18:57.027 に答える
0

問題なく ExceptionHandlerAspect の代わりに OnMethodBoundaryAspect を使用しました。そして、私も自分のものを封印していません。

于 2008-12-09T19:18:28.307 に答える