Writing C# debug output to .txt file

前端 未结 4 992
执念已碎
执念已碎 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 10:05

    Use Trace. It is designed to do what you need.

    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();
        }
    }
    

提交回复
热议问题