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

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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).
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.
It's hardly worth the trouble: it really is just changing Console.Write() to Trace.Write(), and maybe add a system.diagnostics using reference.
There are examples of using System.Diagnostics here: essentialdiagnostics.codeplex.com/… and also here thejoyofcode.com/…
3

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.

7 Comments

Would i need to do this if i follow Joel Coehoorn's solution?
Joel's suggestion appears to be to trace to a file, not a console.
The console would be something i need to be visible, is it possible to combine both yours and Joel's methods together?
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.
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.
|
2

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.

Comments

1

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.

3 Comments

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: 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.
I use/used nlog but moved to Azure. Suddenly things weren't as simple since I had to re-think my file system logging and to log to a cloud storage solution which wasn't straight forward. My view now? System diagnostics doesn't seem such a silly way forward after all.
-1

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.

2 Comments

That does sound very promising but there are other alternatives that are, above all, free.
not free: $5 non-commercial, $49 for a commerical/site license.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.