4

我正在探索一些 AOP,似乎使用 .NET PostSharp 是要走的路。

发生异常时,我想对数据库进行一些简单的日志记录。但是,我发现很难找到任何超越基础的使用 PostSharp 的真实可靠示例。我尝试了以下方法:

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

然后将[LogException]属性附加到方法

但我得到一个编译错误:

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

我不得不承认我对此很陌生,但这似乎是一个有趣的概念,我认为我只需要指出正确的方向

4

2 回答 2

2

我通过扩展它来工作OnExceptionAspect

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

原帖:

它希望您添加 Multicast 属性:

[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

我使用 OnMethodBoundaryAspect 而不是 ExceptionHandlerAspect 没有问题。而且我也没有把我的密封起来。

于 2008-12-09T19:18:28.307 回答