c# - PostSharp:仅包含一些类或一些方法
问题描述
我只需要将日志记录添加到特定类或特定方法。在这里和这里阅读文档我找不到实现目标的方法。
下面列出的是我的测试用例。
namespace ClassLibrary1.C1
{
public class Class1
{
public int Method1()
{
return 42;
}
}
}
namespace ClassLibrary1.C2
{
public class Class2
{
public int Method1()
{
return 42 * 2;
}
}
public class Class3
{
public int Method1()
{
return 42 * 2;
}
public int Method2()
{
return 42 * 3;
}
}
}
装配信息内容
using PostSharp.Patterns.Diagnostics;
using PostSharp.Extensibility;
[assembly: Log(AttributePriority = 1, AttributeExclude = true, AttributeTargetTypes = "ClassLibrary1.*")]
[assembly: Log(AttributePriority = 2, AttributeExclude = false, AttributeTargetTypes = "ClassLibrary1.C2*")]
通过这种方式,我能够从日志中排除整个 Class1,但这只是因为它属于不同的命名空间。
如何仅将Class3.Method2()包含到日志记录中?
解决方案
我使用 AttributeTargetMembers 发布我的解决方案
using PostSharp.Patterns.Diagnostics;
using PostSharp.Extensibility;
[assembly: Log(AttributePriority = 1, AttributeExclude = true, AttributeTargetMembers="Method1")]
[assembly: Log(AttributePriority = 2, AttributeExclude = false, AttributeTargetMembers="Method2")]
[assembly: Log(AttributePriority = 3, AttributeExclude = true, AttributeTargetTypes = "ClassLibrary1.C1*")]
[assembly: Log(AttributePriority = 4, AttributeExclude = true, AttributeTargetTypes = "ClassLibrary1.C2.Class2")]
推荐阅读
- path - Godot:在动画播放器中更改对象的路径
- scala - 尝试从 h2 数据库中检索 json 数据时出错
- r - R - 使用 xml2 读取多个 xml 文件
- node.js - 为什么 API Gateway 没有收到 Lambda 的响应?
- regex - 在非捕获组 RegEx 中捕获电子邮件地址
- javascript - 用数组数据反应三元运算符不起作用?
- python - 如何反转 pygame 颜色?
- c++ - 没有默认构造函数的对象的私有变量
- python - 我正在通过 scrapy-splash 抓取 bet3000 比分直播体育网站,该网站启用了 javascript 的网站,这给了我错误
- java - Jenkinsfile 管道方法的 api 文档/实现在哪里?