开发者

Proper Way to Handle Verbose Console Output in C#

开发者 https://www.devze.com 2023-04-12 00:29 出处:网络
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 i

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 compu开发者_如何学运维ter 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!


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.


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号