将 C# 调试输出写入 .txt 文件
我正在使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用跟踪。它旨在满足您的需求。
Use Trace. It is designed to do what you need.
使用开箱即用的跟踪的最灵活的解决方案是创建一个应用程序配置文件来定义跟踪侦听器。
然后,在您的应用程序中,每当您想要记录某些内容时,只需执行以下操作即可:
但是 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.
Then, in your application, whenever you want to log something, just do:
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.
Ekk 认为 Trace 是一种更好的设计,这一点是正确的,但这并不能回答问题,在没有直接解决方案的情况下这也没什么问题。 OP 或某人可能继承了一个始终使用 Debug 的代码库,并且当时可能不需要 Trace。
我找到了这个解决方案 http://bytes.com/主题/c-sharp/answers/273066-redirect-output-debug-writeline:
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:
你必须做这样的事情:
取自 此处。
另一个相关问题
You will have to do something like this:
Taken from here.
Another related question