CSharp examples for System:Diagnostics
Trace Switch and Trace Listeners
using System.Diagnostics; using System.IO;/*from www . j a va 2 s .c o m*/ class Program { static void Main(string[] args) { Trace.Listeners.Add(new TextWriterTraceListener(File.CreateText("log.txt"))); // text writer is buffered, so this option calls // Flush() on all listeners after writing to them Trace.AutoFlush = true; Debug.WriteLine("Debug says, I am watching!"); Trace.WriteLine("Trace says, I am watching!"); var ts = new TraceSwitch("PacktSwitch", "This switch is set via a command line argument."); if (args.Length > 0) { if (System.Enum.TryParse<TraceLevel>(args[0], ignoreCase: true, result: out TraceLevel level)) { ts.Level = level; } } Trace.WriteLineIf(ts.TraceError, "Trace error"); Trace.WriteLineIf(ts.TraceWarning, "Trace warning"); Trace.WriteLineIf(ts.TraceInfo, "Trace information"); Trace.WriteLineIf(ts.TraceVerbose, "Trace verbose"); } }