[英]Logging and debug messages
I have my debug messages evaluate expensive string operations when printing messages like this: 当打印如下消息时,我的调试消息会评估昂贵的字符串操作:
Log.Debug("here it is" + obj.ExpensiveToString())
Now even if the log level isn't set to Debug
, it still evaluates the expensive string operation. 现在,即使未将日志级别设置为
Debug
,它仍会评估昂贵的字符串操作。
So without having to do this: 因此,无需执行以下操作:
if(debugMode) Log.Debug("here it is" + obj.ExpensiveToString())
or having lots of complex Log.Debug()
methods, how can I get around this? 还是有很多复杂的
Log.Debug()
方法,如何解决呢?
use a compiler directive 使用编译器指令
#if DEBUG
Log.Debug("Here it is: "+obj.ExpensiveToString());
#endif
since these directives increase code verbosity use them for the expensive parts. 因为这些指令增加了代码的冗长性,所以将它们用于昂贵的部分。
Another alternative is to modify or supplement your log system to accept a Func<string>
另一种选择是修改或补充您的日志系统以接受
Func<string>
public void LogDebug(Func<string> evalMe){
#if DEBUG
if (evalMe!=null)
Log.Debug(evalMe);
#endif
}
and then call it like this: 然后这样称呼它:
LogDebug(()=>"Here it is: "+obj.ExpensiveToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.