Search code examples
c#tracerelease-modedebug-mode

Why TextWriterTraceListener can't work in release mode?


This program can work on debug mode but can't work on release mode:

 static void Main(string[] args)
    {
        Trace.Listeners.Add(new TextWriterTraceListener(@"c:\prog\a.txt"));
        Debug.AutoFlush = true;
        Debug.WriteLine("abc");
        Debug.Close();
    }

When this program run in release mode,it can work without error,but can't write line "abc" in a.txt Can you teach me why?Thanks


Solution

  • Because you are using

    Debug.WriteLine("abc")
    

    Which will not compile when building in release mode, use instead :

    Trace.WriteLine("abc")
    

    Note also that Trace wil execute on both modes of building.