将 C# 调试输出写入 .txt 文件

发布于 2024-12-12 20:20:25 字数 150 浏览 0 评论 0原文

我正在使用 .NET Micro Framework 在微控制器上运行代码,并且我想要调试输出写入文本文件。这是如何运作的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

合久必婚 2024-12-19 20:20:25

使用跟踪。它旨在满足您的需求。

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

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();
    }
}
烟酒忠诚 2024-12-19 20:20:25

使用开箱即用的跟踪的最灵活的解决方案是创建一个应用程序配置文件来定义跟踪侦听器。

<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textListener" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="trace.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

然后,在您的应用程序中,每当您想要记录某些内容时,只需执行以下操作即可:

Trace.WriteLine("Hello, this is a trace");

但是 TraceListener 类的强大之处在于其粒度。您可以在错误、信息和警告级别之间进行选择,并为需要跟踪的任何级别定义不同的日志文件。使用配置文件还可以更轻松地在应用程序中禁用跟踪,因为您不需要重新编译应用程序。

有关跟踪系统的详细信息,请查看这篇 MSDN 文章

The most flexible solution for using a out-of-the-box tracing is to make an application configuration file that will define trace listeners.

<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textListener" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="trace.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Then, in your application, whenever you want to log something, just do:

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.

For more informations on tracing system, check this MSDN article.

铁憨憨 2024-12-19 20:20:25

Ekk 认为 Trace 是一种更好的设计,这一点是正确的,但这并不能回答问题,在没有直接解决方案的情况下这也没什么问题。 OP 或某人可能继承了一个始终使用 Debug 的代码库,并且当时可能不需要 Trace。

我找到了这个解决方案 http://bytes.com/主题/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.AutoFlush = true;

Debug.WriteLine("Some Value", "Some Category");
Debug.WriteLine("Some Other Value");

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.AutoFlush = true;

Debug.WriteLine("Some Value", "Some Category");
Debug.WriteLine("Some Other Value");
汹涌人海 2024-12-19 20:20:25

你必须做这样的事情:

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

取自 此处

另一个相关问题

You will have to do something like this:

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

Taken from here.

Another related question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文