Simon Kiely
Simon Kiely

Reputation: 6050

Alternatives to Console.Writeline() for printing debug information from a console application?

I have a C# console application. It is gathering info from several data sources and centralising them in another. Occasionally, it hits some bugs - connection strings stop working; schemas change etc.

At the moment, I am simply printing out debug information using Console.WriteLine(); - this is obviously an awful idea...not least because the application is a scheduled task running every night, so I never see this debug information anyway!

What should I do to allow for simple debugging when the program fails? What is my best practice in this case ?

Thanks very much.

Upvotes: 1

Views: 8474

Answers (6)

mgnoonan
mgnoonan

Reputation: 7200

You want a logging framework, such as log4net or the Enterprise Library Logging Block. These frameworks will allow you to log to a database, log file, or the Event Log for later viewing and retrieval.

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

I would recommend using the EventLog class to write to the application event log (or your own custom log). You could alternatively write to a custom log file of your own choosing. The event log, however has the advantage of easily being viewable remotely from any machine. In addition, if this application is truly unattended, you could have it send an email notification when it fails to the system administrator.

Upvotes: 1

Filip Ekberg
Filip Ekberg

Reputation: 36287

You should read about different logging/error strategies.

I suggest that you start off by using a library like log4net where you can define what kind of format you want for your logs.

Upvotes: 1

Steve Townsend
Steve Townsend

Reputation: 54148

Check out System.Diagnostics. Debug class is a better choice than Console, for starters.

log4net (as linked by @Tim S.) is a useful framework for general diagnostics output, at different levels of severity.

Upvotes: 2

CodeCaster
CodeCaster

Reputation: 151594

Use Debug.WriteLine(). It'll print to the assigned listeners (default only your Visual Studio Output window, but you can hook it up to the Console). If this is for troubleshooting production issues, I'd strongly advise to implement logging in your application.

Upvotes: 3

Tim S.
Tim S.

Reputation: 56536

You can use a logging library. log4net seems to be the most popular choice.

Upvotes: 3

Related Questions