System.Diagnostics.Debug.WriteLine在生产代码中 [英] System.Diagnostics.Debug.WriteLine in production code

查看:149
本文介绍了System.Diagnostics.Debug.WriteLine在生产代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能已经知道了,但我不确定,我没有看到它记录。



我使用System.Diagnostics.Debug.WriteLine通常在开发过程中能够跟踪变量或异常的变化,因为我正在调试代码。这意味着在开发期间让开发和理解更容易发生的事情更容易 。我通常会注释掉代码,或者当我去制作时删除它。



我想知道如果我忘记了代码,会发生什么。比方说,在开发周期中,我正在跟踪使用Debug.Write Line将连接sting记录到输出窗口的错误信息。这显然是在开发的时候,但是我想知道如果我去生活,如果这里有风险。有人可以将调试器附加到我的实时可执行文件并捕获此输出吗?还是只在Visual Studio中产生输出?



当我们从调试切换到发布版本时呢?如果编译发布,编译器会忽略这段代码吗?

解决方案

Debug类中的所有成员都标有< a href =http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx =noreferrer> ConditionalAttribute ,所以调用网站不会被编译成发布版本。


I should probably know this already, but I'm not sure and I don't see it documented.

I use System.Diagnostics.Debug.WriteLine quite often during the development process to be able to track changes to variables or exceptions as I'm debugging the code. This is meant to make development and understanding what's happening easier only during development. I normally either comment out the code or delete it when I go to production.

I'm wondering what happens if I forget to comment the code out. Say, for example, that during the development cycle, I'm tracking error information that may log a connection sting to the output window using Debug.Write Line. This is obviously OK while developing, but I'm wondering if when I go live, if there is a risk here. Can someone attach a debugger to my live executable and trap this output? Or is it something that only produces output in Visual Studio?

And what about when we switch from debug to release? Does this code get ignored by the compiler if we compile for release?

解决方案

All the members in the Debug class are marked with ConditionalAttribute, so the call sites won't be compiled into a Release build.

这篇关于System.Diagnostics.Debug.WriteLine在生产代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆