简体   繁体   English

C#:最佳实践Debug.Print

[英]C#: Best Practices Debug.Print

What is the best practice regarding Debug.Print statements? 有关Debug.Print语句的最佳实践是什么?

Should I litter my class methods with Debug.Print statements or should I avoid Debug.Print altogether? 我应该使用Debug.Print语句Debug.Print我的类方法还是应该完全避免使用Debug.Print

If Debug.Print statements are acceptable should I consider using Trace.Print or EventLog ? 如果Debug.Print语句是可以接受的,我应该考虑使用Trace.PrintEventLog吗?

Are Debug.Print statements necessary with unit tests? 单元测试需要Debug.Print语句吗? Can I avoid Debug.Print statements with well written unit tests? 我可以通过编写良好的单元测试来避免Debug.Print语句吗?

Debug.Print is acceptable, not least because they'll be compiled out in your release builds. Debug.Print是可以接受的,尤其是因为它们将在您的发布版本中编译出来。 "Littering" your code with it, however, doesn't sound especially productive or useful. 然而,用它来“乱丢”你的代码听起来并不是特别有效或有用。

You can add it while debugging a particular area of code. 您可以在调试特定代码区域时添加它。 Once you identify the defect, you can write a unit test to cover the case, fix the bug, and then remove the call to Debug.Print . 一旦确定了缺陷,就可以编写单元测试来覆盖案例,修复bug,然后删除对Debug.Print的调用。

What I sometimes use and leave in the code base is Debug.Assert -- it is like a built-in breakpoint if my application is not in an expected state, which is just an added safety net while doing both automated and manual testing. 我有时使用并留在代码库中的是Debug.Assert - 如果我的应用程序不处于预期状态,它就像一个内置断点,这只是一个额外的安全网,同时进行自动和手动测试。

Debug.Print is not necessary in unit tests, and should not be added for the sake of unit tests. Debug.Print在单元测试中不是必需的,不应为了单元测试而添加。

You don't need Debug.Print at all with unit tests! 完全不需要Debug.Print单元测试! The point of unit tests is that you automatically get an indication whether the test succeeded or failed, you don't need to look for debug prints for that. 单元测试的重点是您自动获得测试成功或失败的指示,您不需要为此查找调试打印。

NUnit is an easy-to-use unit testing library. NUnit是一个易于使用的单元测试库。

As for tracing or logging, you don't need to litter your code with debug prints for that either. 至于跟踪或日志记录,您也不需要使用调试打印来丢弃代码。 Try interception with Castle Windsor or other frameworks that support AOP. 尝试使用Castle Windsor或其他支持AOP的框架进行拦截

I've never used Debug.Print or Trace.Print. 我从未使用过Debug.Print或Trace.Print。 I do write a good amount of unit tests. 我确实写了大量的单元测试。 I've never found that I need to use the Debug or Trace objects with the unit tests that I write. 我从来没有发现我需要将Debug或Trace对象与我编写的单元测试一起使用。 I do try to unit test as much as possible, however. 不过,我确实尝试进行单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM