debug.writeline - 如何在调试时调试它和在实时调用文件

时间:2015-01-01 12:40:10

标签: c# debugging visual-studio-2013

我即将开始使用C#项目,我正在努力解决一个简单的问题:如何将Debug.WriteLine("");转换为输出文件(并且仅当它处于活动状态时?)

我做了什么:我将此添加到app.config

<system.diagnostics>
   <trace autoflush="true">
    <listeners>
      <add name="textListener"
           type="System.Diagnostics.TextWriterTraceListener"
           initializeData="trace.log" />
      <remove name="Default" />
    </listeners>
    </trace>
</system.diagnostics>

然后这是我使用的bug.log课程:

if (System.Diagnostics.Debugger.IsAttached)
{
    Debug.WriteLine(msg);
}
else
{
    Trace.WriteLine(msg);
}

我希望它写入调试窗口,如果它在调试器中(我想要的)并在运行实时(我想要的)时写入输出文件(&#39; trace.log&#39;),但是,无论如何,这两个更改都只是将所有内容放在trace.log中。

如果我没有正常运行,如何设置它以便在Visual Studio(Microsoft Visual Studio Express 2013 for Windows桌面)中的实时版本和trace.log窗口中获得output > debug,但在Visual Studio中使用[ > Start]按钮?

3 个答案:

答案 0 :(得分:2)

如果您的意思是&#34;发布模式&#34;为&#34;生活&#34;与调试模式相反,如何:

#if DEBUG
Debug.WriteLine(msg);
#else
Trace.WriteLine(msg);
#endif

您当然可以使用预编译器中的实际调试常量替换DEBUG。

答案 1 :(得分:2)

保持简单:

Debug.WriteLine(msg);
Trace.WriteLine(msg);

您始终会追踪到日志。 如果附加了调试器,您还可以登录调试器窗口。如果要查看调试器输出,请确保您的解决方案[ > Start]实际上附加了调试器。请参阅 How to: Change the Start Action for Application Debugging

答案 2 :(得分:2)

.configrelease配置可以有两个不同的debug文件,在发布配置中添加一个新的侦听器来跟踪文件。要管理多个配置文件,请查看Scott Hanselman文章 Managing Multiple Configuration File Environments with Pre-Build Events