【发布时间】:2011-07-04 08:23:10
【问题描述】:
仅当应用程序在调试模式下运行时,我才需要记录消息。我找到了两种方法:
第一:需要记录的时候需要到处写3行。但是,Logger 语句仅在编译时被禁用,这正是我需要的。 Logger.Log 根本不会被执行。
#if DEV_ENV
Logger.Log("Application started !"); // This line is grayed. Perfect !
#endif
public static void Log(string message)
{
Debug.WriteLine(message);
}
第二:非常整洁。需要记录的地方只有一行代码。不确定是否执行 Logger.Log 语句。 如果仅在编译时删除函数调用(与第一种方法相同。但是,现在确定代码行没有变灰),我想这样做。
Logger.Log("Application started !"); // This line is not grayed out. But, function is not called. So, confused whether its removed at compile time.
[Conditional("DEV_ENV")]
public static void Log(string message)
{
Debug.WriteLine(message);
}
我担心性能差异。
【问题讨论】:
标签: c# performance compiler-directives