简体   繁体   English

在发布System.Diagnostics之前删除日志

[英]Remove Log before release System.Diagnostics

To Log output I am using the following code. 要记录输出我使用以下代码。

 System.Diagnostics.Debug.WriteLine("Hello");

Now, its always advisory to remove such logs before the app is submitted. 现在,它始终建议在提交应用程序之前删除此类日志。

So, is that we have to remove such lines, before we submit it for release or its done implicitly. 那么,在我们提交它以供发布或隐式完成之前,我们是否必须删除这些行。

Is there any another better way to log output in C#, which removes the logging to the console when its released. 还有另一种更好的方法来记录C#中的输出,这会在发布时删除记录到控制台。 I see Log4Net is one of them. 我看到Log4Net就是其中之一。

All methods on the System.Diagnostics.Debug class have the ConditionalAttribute , so under most compilers they will not be compiled into a Release build (unless you define the DEBUG attribute in the release build). System.Diagnostics.Debug类中的所有方法都具有ConditionalAttribute ,因此在大多数编译器中,它们不会被编译为Release构建(除非您在发布版本中定义DEBUG属性)。 1 1

This is certainly true for the compilers within Visual Studio. 对于Visual Studio中的编译器来说,这当然是正确的。

Your second question about log4Net is actually the reverse, and something to be careful about if you do decide to start using log4Net - log4Net debug calls are included within debug builds and are emitted if you have the logger set to the debug trace level (usually done with runtime configuration). 关于log4Net的第二个问题实际上是相反的,如果你决定开始使用log4Net,需要注意的事项 - 如果你将logger设置为调试跟踪级别,则会发出log4Net调试调用并发出log4Net调试调用(通常已完成)运行时配置)。


1. The MSDN pages are actually (IMO) a little bit unclear, but these SO posts agree with my interpretation: 1. MSDN页面实际上(IMO)有点不清楚,但这些SO帖子与我的解释一致:

System.Diagnostics.Debug.WriteLine in production code 生产代码中的System.Diagnostics.Debug.WriteLine

C# Do Debug statements get compiled out when running in Release mode? 在Release模式下运行时,C#Do Debug语句会被编译出来吗?

You can use preprocessor directive: 您可以使用预处理器指令:

#if DEBUG
    System.Diagnostics.Debug.WriteLine("Hello");
#endif

That line will be skipped when you'll build your application in Release build configuration. 当您在Release构建配置中构建应用程序时,将跳过该行。

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

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