我正在尝试将调试日志添加到我的 C#.net 代码中。 但它弄乱了我的代码,它看起来像 hell 。 有什么东西可以自动记录每个代码及其值吗? 现在看起来像这样
#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
最佳答案
你应该使用 ConditionalAttribute
.它允许您定义条件方法,如果条件不匹配,这些方法将在构建时删除:
[Conditional("DEBUG")]
void DebugLog(string message)
{
// Do some logging...
}
在未定义 DEBUG
的构建中将删除对该方法的调用。就像
#if DEBUG
DebugLog("Testing...");
#endif
与移动 #if
inside 方法相比,这样做的额外好处是不会弄乱堆栈跟踪。
关于c# - 调试日志弄乱了我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22379905/