views:

190

answers:

5

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

+1  A: 

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.

Mitch Wheat
+3  A: 

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.

Joel Coehoorn
Additionally, you can use the System.Diagnostics.Debug to do tracing that is only enabled when performing a debug compilation (technically whenever the DEBUG compiler constant is set).
gWiz
Thankyou Joel, is there any particular place that i can find out more on how to use it? Something as simple as a basic tutorial would be great.
Jamie Keeling
It's hardly worth the trouble: it really is just changing Console.Write() to Trace.Write(), and maybe add a system.diagnostics using reference.
Joel Coehoorn
+2  A: 

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.

Noon Silk
Why not compare/contrast this recommendation with the asker's consideration of using System.Diagnostics. For one, you get System.Diagnostics for free; log4net is a separate assembly. Also, because it's part of the BCL, chances are more .NET devs know how to use System.Diagnostics which could benefit project maintenance.
gWiz
gWiz: You're free to do that in your own answer :) Personally I don't see the point; there is no doubt, in my mind, that the best approach is to go for log4net. Perhaps I could explain, in detail, why, but I leave that as an exercise that can be done with research :) Not everything needs to be hand-fed to people, and certainly, I don't have the patience to do it.
Noon Silk
+2  A: 

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.

Hans Passant
Would i need to do this if i follow Joel Coehoorn's solution?
Jamie Keeling
Joel's suggestion appears to be to trace to a file, not a console.
Hans Passant
The console would be something i need to be visible, is it possible to combine both yours and Joel's methods together?
Jamie Keeling
You'll have to make up your mind about what you want. Mine was $0.25, anything you write with Console.WriteLine() will be visible in the console window. If you want to use Joel's $1.00 solution, you'll have to setup the Trace class in your app.exe.config file to also trace to the console. Ask Joel if anything about that was unclear.
Hans Passant
Thankyou very much for the help nobugz, i'll mark your answer up one vote but im sure you can understand me in giving Joel the answer to my question.
Jamie Keeling
Hmya, such is the price of a cheap solution. The right one was silky's btw.
Hans Passant
But for my particular use i did not want to use additional third party extensions, although i will consider using it in the future.
Jamie Keeling
+1  A: 

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.

TheObjectGuy
That does sound very promising but there are other alternatives that are, above all, free.
Jamie Keeling