我有很多类调用 Trace.WriteLine("LogSomethingClassSpecific"),在各个地方都使用各种方法。现在我希望其中一些类像这样进行所有 Trace 调用:Trace.WriteLine("LogSomethingClassSpecific", "CategoryA")
但是!我想通过单个分类器类从外部实现这一点;无需查找和修改每个特定类中的每个 Trace 调用。
示例架构
可能会或可能不会使用 Trace 调用的 Class1、Class2、Class3
单独的 ControllerClass 使 Class2 和 Class3 中的所有现有 Trace 调用都使用添加的参数“CategoryA”进行调用。同时保留轻松更改目标类的灵 active 。
如何实现?
是否可以将某些属性与面向方面编程 (AOP) 库结合使用? 例如 控制类可以specify which classes to target :
[assembly: Categorizer("CategoryA", AttributeTargetTypes = "Namespace.ClassB")]
然后 intercept all Trace.WriteLine calls with PostSharp
但我不知道有什么方法可以提取调用上下文来确定调用类是否已被标记为包含“CategoryA”?
或者有其他方法可以实现这个目标吗?
感谢任何想法。
最佳答案
有几种方法可以解决这个问题,但我假设您在 if/else 构造之类的地方调用 Trace,所以这里是您将如何按照您的要求去做(至少,什么我想你是在问)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using System.Diagnostics;
[assembly: ConsoleApplication2.TraceIntercept(AttributeTargetAssemblies = "System", AttributeTargetTypes = "System.Diagnostics.Trace")]
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ExampleA ex = new ExampleA();
ex.Method1();
Console.ReadKey();
}
}
public class ExampleA
{
public void Method1()
{
Trace.Write("Test");
}
}
[Serializable]
[TraceIntercept(AttributeExclude = true)]
public class TraceIntercept : MethodInterceptionAspect
{
private bool addArgument;
private string typeName = string.Empty;
public override void OnInvoke(MethodInterceptionArgs args)
{
CheckInvocationPoint();
if (addArgument)
{
//Do work. Change arguments, etc.
}
args.Proceed(); // Proceed with Trace
}
private void CheckInvocationPoint()
{
if (string.IsNullOrEmpty(this.typeName))
{
StackTrace s = new StackTrace();
StackFrame f = s.GetFrame(2);
string className = f.GetMethod().DeclaringType.Name;
if (classsName.Equals("ExampleA"))
{
addArgument = true;
}
}
}
}
}
这将拦截对 System.Diagnostics.Trace 方法的调用,而是调用 TraceIntercept.OnInvoke 方法,您可以在其中操作 Trace 调用。其工作方式是 PostSharp 将简单地用对方面的调用替换对 Trace.Write 的调用。
编辑:据我所知,没有办法获取目标方法的实际调用点。这意味着您必须在运行时进行一些反射。我已经更新了代码以使用堆栈跟踪。您只需(针对每种类型)执行此操作一次,因为方面生命周期针对每种类型而不是针对每种实例,因此您只需点击一次。我不会这样做,但我认为这将用于调试目的。
关于c# - 向多个目标类中的方法调用注入(inject)外部参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319698/