问题描述
我正在尝试一些 AOP,似乎使用 .NET PostSharp 是可行的方法.
I am adventuring into some AOP and it seems with .NET PostSharp is the way to go.
我想在发生异常时对数据库做一些简单的日志记录.但是,我发现很难找到超出基础知识的 PostSharp 使用实例.我尝试了以下方法:
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
}
}
然后将 [LogException]
属性附加到方法
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
推荐答案
我通过扩展 OnExceptionAspect
使其工作:
I got it to work by extending the OnExceptionAspect
:
[Serializable]
public sealed class LogExceptionAttribute : OnExceptionAspect
{
public override void OnException(MethodExecutionEventArgs eventArgs)
{
//do some logging here
}
}
原帖:
它想让你添加多播属性:
It wants you to add the Multicast attribute:
[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/多播/Overview.html
See this link for more details:http://doc.postsharp.org/1.0/UserGuide/Laos/Multicasting/Overview.html
这篇关于使用 PostSharp 添加 OnException 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!