请问在PostSharp中,我是否可以在运行时打开/关闭跟踪?我需要写更少的代码,所以最近我只是将其删除了。跟踪功能是暂时需要的。
也许有一种带有运行时开关功能的替代品可以使用吗?
更新1:我想到了一个主意,但不知道是否好用。以下是一个例子。
public class Smth
{
private long i = 0;
[TraceAttribute]
public void Execute()
{
Console.WriteLine("Execute call" + i++);
Thread.Sleep(200);
}
}
[Serializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
if(Manager.IsEnabled)
{
Console.WriteLine();
Console.WriteLine(string.Format("Entering {0}", args.Method.Name));
}
}
public override void OnExit(MethodExecutionArgs args)
{
if(Manager.IsEnabled)
{
Console.WriteLine(string.Format("Leaving {0}", args.Method.Name));
Console.WriteLine();
}
}
}
public static class Manager
{
public static bool IsEnabled { get; set; }
static Manager()
{
IsEnabled = true;
}
}
通过更改“IsEnabled”属性,我能够打开/关闭跟踪... 有其他建议吗?