8.1. Providing Fine-Grained Control over Debugging/ Tracing Output
Problem
Your application consists of multiple components. You need, at specific times, to turn on debug/trace output for a select few components, while leaving all other debug/trace output turned off. In addition, you need control over the type and amount of information that is produced by the Trace/Debug statements.
Solution
Use the BooleanSwitch class with an application configuration file (*.config). The following method creates three switches for your application: one that controls tracing for database calls, one that controls tracing for UI components, and one that controls tracing for any exceptions that are thrown by the application:
public class Traceable
{
BooleanSwitch DBSwitch = null;
BooleanSwitch UISwitch = null;
BooleanSwitch exceptionSwitch = null;
[System.Diagnostics.ConditionalAttribute("TRACE")]
public void EnableTracing()
{
DBSwitch = new BooleanSwitch("DatabaseSwitch", "
"Switch for database tracing");
Console.WriteLine("DBSwitch Enabled = " + DBSwitch.Enabled);
UISwitch = new BooleanSwitch("UISwitch",
"Switch for user interface tracing");
Console.WriteLine("UISwitch Enabled = " + UISwitch.Enabled);
exceptionSwitch = new BooleanSwitch("ExceptionSwitch",
"Switch for tracing thrown exceptions");
Console.WriteLine("ExceptionSwitch Enabled = " + exceptionSwitch. Enabled);
}
}After creating each switch, the Enabled property is displayed, indicating whether the switch is on or off.
Creating these switches ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access