PostSharp:包括一些 类 或仅包括一些方法
PostSharp: Include some classes or some methods only
我只需要将日志记录添加到特定 class 或特定方法。
阅读文档 here and here 我找不到实现目标的方法。
下面列出了我的测试用例。
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;
}
}
}
AssemblyInfo 内容
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() 仅包含在日志记录中?
我 post 我的解决方案使用 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")]
@ilCosmico 的回答是正确的。另一种包含特定成员的方法是向成员声明添加 [Log]
属性。例如:
...
namespace ClassLibrary1.C2
{
...
public class Class3
{
...
[Log]
public int Method2()
{
return 42 * 3;
}
}
}
我只需要将日志记录添加到特定 class 或特定方法。 阅读文档 here and here 我找不到实现目标的方法。
下面列出了我的测试用例。
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;
}
}
}
AssemblyInfo 内容
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() 仅包含在日志记录中?
我 post 我的解决方案使用 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")]
@ilCosmico 的回答是正确的。另一种包含特定成员的方法是向成员声明添加 [Log]
属性。例如:
...
namespace ClassLibrary1.C2
{
...
public class Class3
{
...
[Log]
public int Method2()
{
return 42 * 3;
}
}
}