【发布时间】:2015-02-04 19:08:29
【问题描述】:
我正在尝试将调试日志添加到我的 C#.net 代码中。 但它弄乱了我的代码,看起来像地狱。 有什么东西可以自动记录每个代码及其值吗? 现在看起来像这样
#if DEBUG
debuglogger("The functionstarted");
#endif
someCode1();
#if DEBUG
debuglogger("SomeCode1 Finished");
#endif
someCode2();
#if DEBUG
debuglogger("SomeCode2 Finished");
#endif
someCode3();
#if DEBUG
debuglogger("SomeCode3 Finished");
#endif
#if DEBUG
debuglogger("Function End");
#endif
【问题讨论】:
-
将
#if DEBUG放在 debugLogger 函数中怎么样? -
您可以创建一个函数
debugLoggerPrint来打印字符串,然后您只需要在该函数中使用一次#if DEBUG并从您的代码中正常调用它。 -
或者是时候看看面向方面的编程了...
-
是的,如果您想完全从主代码中提取日志记录逻辑,PostSharp 之类的东西可能会有所帮助。
-
@Baldrick 该方法仍然会使您的堆栈混乱,最好使用
ConditionalAttribute。