简体   繁体   中英

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.

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.

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.

Thanks

Look at System.Diagnostics.Trace. You can add different TraceListeners to it, including listeners for the Console or files. Then replace all your Console.Write()/Console.WriteLine() calls with Trace.Write()/Trace.WriteLine() and you're good. You can even implement your own TraceListener (it's very easy) to send the messages to your GUI app.

The $0.25 solution is Project + Properties, Application tab, 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.

Use DebugView from SysInternals to capture debug output. This is a separate GUI application that captures trace /debug output.

This post, Using DebugView and C#, shows an example.

I recommend you start using log4net as soon as possible; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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