我冒险进入一些AOP,似乎.NET PostSharp是要走的路.
我想在发生异常时对数据库进行一些简单的日志记录.然而,我发现很难找到任何超出基础知识的使用PostSharp的真实例子.我尝试了以下方法:
[Serializable] public sealed class LogExceptionAttribute : ExceptionHandlerAspect { public override void OnException(MethodExecutionEventArgs eventArgs) { //do some logging here } }
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。