Reputation: 520
Can anyone tell me, if in PostSharp, I somehow can turn on/off tracing in runtime? I need to write less code, so lately I could simply remove it. Tracing functionality is temporarily required.
Maybe there's an alternative to PostSharp with runtime on/off feature?
Update 1: I've come up with idea, but I don't know if it's good. Here's an example
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;
}
}
By changing IsEnabled property I was able to turn on/off tracing... Any other suggestions?
Upvotes: 4
Views: 1289
Reputation: 14585
What you have is fine (as long as it's temporary). Easily apply the aspect using multicasting. See my articles http://www.sharpcrafters.com/blog/post/Day-2-Applying-Aspects-with-Multicasting-Part-1.aspx and http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx
Disclaimer: I'm assuming the OP wants to do this without a rebuild since he said runtime which leads me to believe he wants a config setting. Otherwise I would suggest a much different route
Upvotes: 1