Debug
Object –> Debug
Prefer ILogger over System.Diagnostics.Debug and System.Diagnostics.Trace.
Methods and properties to debug code.
This class can only be used when the application is compiled in DEBUG mode.
enabling debugging
Either:
- Add
#define DEBUG(or#define TRACE) to the top of the file, or; - Add the
/d:DEBUGor/d:TRACEflag when compiling.
adding trace listeners
Trace Listeners are shared by both Debug and Trace and used to output debug/trace information:
Using a TextWriterTraceListener
Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
Debug.AutoFlush = true;
Debug.WriteLine("message");
Using DefaultTraceListener
Trace.Listeners.Clear(); // Remove the default trace listener.
DefaultTraceListener defaultListener = new();
Trace.Listeners.Add(defaultListener); // This also automatically adds the listener to Debug.Listeners.
defaultListener.LogFileName = filename;
Debug.WriteLine("message");