在发布模式下写入调试
我有一个需要吞下的异常(日志记录期间的异常),但是我不希望异常信息完全丢失在时间的迷雾中,所以我想我至少可以使用
Debug.Write(ex.ToString());
这种方式将其输出到调试,如果它成为必要的支持,至少可以在有问题的机器上使用 DebugView。
问题是 Debug 类在发布模式下被删除 - 如何在发布模式下输出要调试的内容?
I have an exception that I need to swallow (exception during logging), however I dont want the exception information to be completely lost to the mists of time and so I figured I may as well at least output it to debug using
Debug.Write(ex.ToString());
That way if it becomes necessary support can at least use DebugView on the machine having problems.
Trouble is the Debug class is removed in release mode - how do I output something to debug whilst in release mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用
这与 Debug.Write(ex.ToString()); 的作用相同,但不会在发布模式下删除(只要您没有删除 TRACE 常量的定义)您的项目设置)
Simply use
This does the same as
Debug.Write(ex.ToString());
but won't be removed in Release mode (as long as you didn't remove the definition of the TRACE constant in your project settings)也许值得研究一些日志框架。 我更喜欢 log4net,它有不同的日志记录级别(DEBUG、INFO、WARN、ERROR)、不同的记录器(您可以为应用程序的每个重要部分放置一个记录器),并且只需更改配置文件。 因此,如果代码的某些区域出现问题,您可以为该记录器设置调试级别,完成后可以恢复原始级别。
这只是表面现象,还有更多功能,例如发送错误电子邮件或记录到数据库。
Maybe it would be worth looking into some logging framework. I prefer log4net where you have different logging levels (DEBUG, INFO, WARN, ERROR), different loggers (you can put one logger for each important part of the application), and you can set different debug levels for different loggers just by changing the config file. So if you have a problem in some area of the code you can set DEBUG level for that logger(s) and when you're done you can put the original level back.
This just scratches the surface, there are many more features, like sending email on errors, or logging to DB.