c# – 使用PostSharp添加OnException属性
发布时间:2020-12-15 22:56:20 所属栏目:百科 来源:网络整理
导读:我冒险进入一些AOP,似乎.NET PostSharp是要走的路. 我想在发生异常时对数据库进行一些简单的日志记录.然而,我发现很难找到任何超出基础知识的使用PostSharp的真实例子.我尝试了以下方法: [Serializable]public sealed class LogExceptionAttribute : Except
我冒险进入一些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:workCoDrivrBeta1CoDrivrBeta1EXEC CoDrivrBeta1 我必须承认我对此很新,但这似乎是一个有趣的概念,我想我只需指向正确的方向 解决方法
我通过扩展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 } } 有关详细信息,请参阅此链接: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |