---------- public static void SetDebugOutput(string sFile) { Debug.Listeners.Clear(); TextWriterTraceListener listener1 = new TextWriterTraceListener(File.CreateText(sFile)); Debug.Listeners.Add(listener1); Debug.AutoFlush = true; } string s1 = @".\App_Data"; if (!Directory.Exists(s1)) Directory.CreateDirectory(s1); CCommon.SetDebugOutput(string.Format(@".\App_Data\Debug-{0}.txt", gcsProjectID)); ---------- make an application configuration file that will define trace listeners: then: Trace.WriteLine("Hello, this is a trace"); But the power of the TraceListener class lies into its granularity. You can chose between Error, Info and Warning levels and define different log file for whatever level you need to trace. Using configuration files makes it also easier to disable tracing in your application because you don't need to recompile your application. ---------- To log debug message to a file: TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(tr1); TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt")); Debug.Listeners.Add(tr2); Debug.Unindent(); Debug.WriteLine("Debug Information-Product Ending"); Debug.Flush(); same as above: Debug.Listeners.Clear(); sFile = DateTime.Now.Second.ToString(); sFile = string.Format("{0}-{1}.log", ProjectID, sFile.Substring(sFile.Length - 1, 1)); TextWriterTraceListener tr2 = new TextWriterTraceListener(File.CreateText(sFile)); Debug.Listeners.Add(tr2); Debug.AutoFlush = true; ---------- debug output to write to a text file: using System; using System.Diagnostics; class Test { static void Main() { Trace.Listeners.Add(new TextWriterTraceListener("yourlog.log")); Trace.AutoFlush = true; Trace.Indent(); Trace.WriteLine("Entering Main"); Console.WriteLine("Hello World."); Trace.WriteLine("Exiting Main"); Trace.Unindent(); Trace.Flush(); } } ---------- // set up listener string filename = @"C:\listener.txt"; FileStream traceLog = new FileStream( filename, FileMode.OpenOrCreate ); TextWriterTraceListener listener = new TextWriterTraceListener( traceLog ); // output to listener listener.WriteLine( "Trace message here" ); // flush any open output before termination // maybe in an override of Form.OnClosed listener.Flush();