Writing C# debug output to .txt file

前端 未结 4 993
执念已碎
执念已碎 2020-12-14 09:11

I\'m running code on a microcontroller with .NET Micro Framework, and I want my debug output to write to a text file. How does this work?

4条回答
  •  一整个雨季
    2020-12-14 09:59

    Ekk is right about Trace being a better design, however that doesn't answer the question, which would be fine in the absence of a direct solution. The OP or someone may have inherited a code base which uses Debug throughout, and Trace may not be desirable at the time.

    I found this solution http://bytes.com/topic/c-sharp/answers/273066-redirect-output-debug-writeline:

    TextWriterTraceListener[] listeners = new TextWriterTraceListener[] {
    new TextWriterTraceListener("C:\\debug.txt"),
    new TextWriterTraceListener(Console.Out)
    };
    
    Debug.Listeners.AddRange(listeners);
    
    Debug.WriteLine("Some Value", "Some Category");
    Debug.WriteLine("Some Other Value");
    

提交回复
热议问题