在 C# 中处理详细控制台输出的正确方法
通常,当我开发程序时,我会抛出一些 console.writeline 语句,让我知道应用程序在调试期间正在做什么。我经常发现自己删除它们并重新添加它们以保持代码整洁。我还没有找到一种看似有效的方法来将它们优雅地放置在代码中。
我想知道其他人做了什么或者什么被认为是最佳实践。我不确定是否可以使用跟踪,因为我没有在安装了 Visual Studio 的计算机上进行调试(我发布到的服务器没有它)。
有没有基于AOP的方法来攻击这个?理想情况下,我不希望在生产代码中输出,因为写入控制台似乎会减慢应用程序的速度。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否考虑过使用Log4Net?它对你来说应该更加灵活。您可以在代码中保留大量日志记录,并根据执行时加载的配置打开或关闭它。
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.
那里有很多注销选项。 MS 企业库和 log4net 是两个非常流行的选择。在每一个中,您都可以通过配置文件控制日志记录的级别,以便在开发中您可以进行非常详细的日志记录,但在生产中您可以抑制除错误之外的所有内容 - 所有这些都无需更改代码。
此外,如果您需要调试生产问题,您只需更改配置设置即可在生产中打开详细日志记录。
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 的企业库的 日志应用程序块 可能是选择。
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: