Using Switches to Control Debug and Trace : Debug : Development C# Examples


C# Examples » Development » Debug »

 

Using Switches to Control Debug and Trace









    
//  compile  with:  csc  /D:DEBUG  /r:system.dll  boolean.cs
using  System;
using  System.Diagnostics;

class  MyClass
{
        public  MyClass(int  i)
        {
                this.i  =  i;
        }
        
        [Conditional("DEBUG")]
        public  void  VerifyState()
        {
                Debug.WriteLineIf(debugOutput.Enabled,  "VerifyState  Start");
                
                if  (debugOutput.Enabled)
                        Debug.WriteLine("VerifyState  End");
        }
        
        BooleanSwitch  debugOutput  =    new  BooleanSwitch("MyClassDebugOutput",  "Control  debug  output");
        int  i  =  0;
}

class  MainClass
{
        public  static  void  Main()
        {
                Debug.Listeners.Clear();
                Debug.Listeners.Add(new  TextWriterTraceListener(Console.Out));
                MyClass  c  =  new  MyClass(1);
                
                c.VerifyState();
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» Debug