简体   繁体   English

使用System.Diagnostics进行简单的调试和日志记录

[英]Simple Debug and Logging using System.Diagnostics

I wish to be able to write entries to a console application which will describe when actions have been completed, possibly writing them to a .txt file at one point. 我希望能够将条目写入控制台应用程序,该应用程序将描述操作何时完成,可能在某一时刻将它们写入.txt文件。

I would like it to be used with a separate GUI application running at the same time so i can use the application and monitor the log simultaneously. 我希望它与同时运行的单独GUI应用程序一起使用,这样我就可以使用应用程序并同时监视日志。

I only assume the Diagnostic class is the right tool to use however I have never used any logging methods before, so i welcome any other suggestions. 我只假设Diagnostic类是正确使用的工具,但我之前从未使用过任何日志记录方法,所以我欢迎任何其他建议。

Thanks 谢谢

Look at System.Diagnostics.Trace. 查看System.Diagnostics.Trace。 You can add different TraceListeners to it, including listeners for the Console or files. 您可以向其添加不同的TraceListener,包括控制台或文件的侦听器。 Then replace all your Console.Write()/Console.WriteLine() calls with Trace.Write()/Trace.WriteLine() and you're good. 然后使用Trace.Write()/ Trace.WriteLine()替换所有的Console.Write()/ Console.WriteLine()调用,你很好。 You can even implement your own TraceListener (it's very easy) to send the messages to your GUI app. 您甚至可以实现自己的TraceListener(非常简单)将消息发送到GUI应用程序。

The $0.25 solution is Project + Properties, Application tab, Output type = Console Application. $ 0.25解决方案是Project + Properties,Application选项卡,Output type = Console Application。 Now you've got a console window as well as your regular UI. 现在你有了一个控制台窗口和常规用户界面。 Anything you write with Console.WriteLine() will end up on that console window. 使用Console.WriteLine()编写的任何内容都将在该控制台窗口中结束。

Use DebugView from SysInternals to capture debug output. 使用SysInternals中的DebugView捕获调试输出。 This is a separate GUI application that captures trace /debug output. 这是一个单独的GUI应用程序,可捕获跟踪/调试输出。

This post, Using DebugView and C#, shows an example. 这篇文章, 使用DebugView和C#,展示了一个例子。

I recommend you start using log4net as soon as possible; 我建议你尽快开始使用log4net ; it's fairly trivial to use (though setting up is slightly complex, you need to make a few config entries), and it can be quite a beautiful system. 使用它是相当简单的(虽然设置稍微复杂,你需要制作一些配置条目),它可以是一个非常漂亮的系统。

Here's my admittedly self-serving answer: use my logging framework . 这是我公认的自负答案:使用我的日志框架 Unlike some other logging frameworks, it's extremely easy to use and configure. 与其他一些日志框架不同,它非常易于使用和配置。 It also has a very small footprint. 它的占地面积也非常小。 In addition, it comes with a small application that you can use to view your logs in real-time. 此外,它还附带了一个小应用程序,您可以使用它来实时查看日志。 It sounds to me like it's everything you need. 这听起来像是你需要的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM