tags:

views:

252

answers:

3

I am currently refactoring an application that prints its status to the console window. At the moment I am doing something like this:

 Console.Write("Print some status.....")
 //some code 
 Console.WriteLine("Done!")

Now while this works fine, all the logic is hidden between console.writelines and I find makes it very hard to read.

I don't know if there is a better way of doing this, but I just wanted to ask and see if anyone has come up with a better/more clean way of print application status to the console.

Any ideas?

A: 

Why not use a Logger object that write errors into a text file? You could come with some "priority" error messages such as: Logger.print(new priority("important"), "blabla"); This way, you could find in your file the exact time and all the message you want.

If you absolutely want the console, you could use the priority on the console.. so it would only prints what you tell the logger to print, such as network error, etc..

+3  A: 

Take a look at Log4Net, it handles everything, but might be an overkill for your app, no idea. However knowing Log4Net will likely help you down the road someday so maybe this is a good chance too learn it.

Robert Gould
+1  A: 

I second using Log4Net. It is pretty easy to use it without invoking the difficult parts - just do the following: In your applications Main() method, call

log4net.Config.BasicConfigurator.Configure(new log4net.Appender.ConsoleAppender());

That sets up a basic Console logger that logs all messages to stdout.

In the class that needs logging, create a new ILog like so:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof (MyClass));

Then in the method that needs logging, call

log.Debug("Print Some status ...");

Once you have all of this set up and working. look through the Log4Net documentation on how to set up more useful logging. You can do a lot of different types of logging without changing the logging calls in your code at all.

Jamie Penney