Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 319
Jeffrey Kevin Pry Avatar asked Oct 07 '11 15:10

Jeffrey Kevin Pry


2 Answers

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.

like image 145
Jon Skeet Avatar answered Sep 17 '22 23:09

Jon Skeet


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.

like image 44
HitLikeAHammer Avatar answered Sep 20 '22 23:09

HitLikeAHammer