c# - 调试日志弄乱了我的代码

标签 c# windows debugging

我正在尝试将调试日志添加到我的 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/

相关文章:

asp.net - "unable to start debugging on the web server"MSVSMON.EXE 无法启动。 VS2010 错误

c# - 当用户清理 WPF 中的文本字段时,文本框未将值设置为 null

c# - 在现有 SqlConnection 中打开 DbContext 连接

c# - 使用 JsonConvert 进行序列化会丢失数据

c# - 如果没有等待最后一个 MoveNextAsync() 任务,IAsyncEnumerator<T>.DisposeAsync() 是否应该抛出异常?

windows - 批量设置命令的输出和错误以分隔变量

windows - 为了可读性,我应该在 Notepad++ 中使用什么语言选项和我的 Windows 主机文件?

windows - 如何隐藏批处理输出

eclipse - 增加 Eclipse 调试器 toString 缓冲区大小

c# - 在 Debug模式下单步执行时跳过方法的属性