Jeffrey Kevin Pry
Jeffrey Kevin Pry

Reputation: 3296

Proper Way to Handle Verbose Console Output in C#

Often when I develop a program I throw some console.writeline statements to let me know what the application is doing during debugging. Often time I find myself removing them and adding them back in in order to keep the code clean. I haven't found a seemingly efficient way to place these gracefully in the code.

I was wondering what other people do or what is considered a best practice for this. I am not sure if I can use trace or not because I am not debugging on a computer with Visual Studio installed (the server I am publishing to does not have it).

Is there any AOP based method to attack this? Ideally I would not want the output in production code because writing to the console seems to slow down the application.

Thanks in advance!

Upvotes: 6

Views: 4566

Answers (4)

HitLikeAHammer
HitLikeAHammer

Reputation: 2695

There are many options for logging out there. MS enterprise library and log4net are two very popular options. In each of these you can control the level of logging through the config file so that in dev you can have very verbose logging but in prod you can suppress all but the errors for example - all without changing code.

Also, if you need to debug a production problem you can turn verbose logging on in production simply by changing the config settings.

Upvotes: 3

Arun
Arun

Reputation: 2533

Microsoft's Enterprise Library's Logging Application Block could be a choice.

Upvotes: 2

Jesse
Jesse

Reputation: 8393

Have you tried writing only when in debug mode? I believe the correct way of doing so is like so:

#if DEBUG
  Console.WriteLine("Debug stuff here");
#endif

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504122

Have you looked at using Log4Net? It should be a lot more flexible for you. You can leave a lot of logging in the code, and turn it on or off based on a configuration loaded at execution time.

Upvotes: 8

Related Questions