使用PostSharp添加OnException属性 [英] Adding an OnException attribute using PostSharp

查看:88
本文介绍了使用PostSharp添加OnException属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些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]属性附加到方法

但是出现编译错误:

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/Multicasting/Overview.html

See this link for more details: http://doc.postsharp.org/1.0/UserGuide/Laos/Multicasting/Overview.html

这篇关于使用PostSharp添加OnException属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆