我正在尝试一些 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
我必须承认我对此很陌生,但这似乎是一个有趣的概念,我想我只需要指出正确的方向
我通过延长它的工作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 http://doc.postsharp.org/1.0/UserGuide/Laos/Multicasting/Overview.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)