【问题标题】:Printing newline using System.Diagnostics.Debugger.Log使用 System.Diagnostics.Debugger.Log 打印换行符
【发布时间】:2011-10-02 09:38:18
【问题描述】:

在调试时,我在日志中打印了很多行:

System.Diagnostics.Debugger.Log(0, null, responseFromServer);
System.Diagnostics.Debugger.Log(0, null, t[0]); 
....

它们都在同一行打印。我怎样才能让它们在单独的行中打印?

我尝试过使用

System.Diagnostics.Debugger.Log(0, null, t[0]+"\n");

但是,它没有用。任何帮助将不胜感激 。谢谢

【问题讨论】:

    标签: c# system.diagnostics


    【解决方案1】:

    在 .NET 中,成语 Environment.NewLine 是一个 System.String,它由正确的 System.Char(s) 组成以终止一行文本,因此:

    System.Diagnostics.Debugger.Log(0, null, t[0] + Environment.NewLine);
    

    [2015-05-07 更新]

    几年后回想起来,我觉得我在这方面至少有一点失误(尽管,我确实认为能够做低级别的工作很重要NewLine 有时也不必与语言打架;所以我也喜欢原来的答案...)

    首先,David Brown 确实在下面给出了一个很好的答案:改用 System.Diagnostics.Debug.WriteLine(和 Write)。这 一个很好的解决方案,尤其是在 OP 的情况下,因为调用的其他参数甚至没有真正被使用; Debug.Write/WriteLine 调用看起来像这样(例如使用 OP 的原始调用,假设 OP 的原始第一个参数 responseFromServer 已经终止,第二个需要终止):

    System.Diagnostics.Debug.Write(responseFromServer);
    System.Diagnostics.Debug.WriteLine(t[0]);
    

    简单易懂。

    更好的是,为什么不Trace

    我只是将您指向this stackoverflow question here,但这是要点。

    You can set up in your App.config but, of course you can also just create it all programatically as well, since the app.config sections simply create objects!

    类似:

    【讨论】:

    • Debug.WriteLine 不是换行,这是bug吗?
    【解决方案2】:

    根据System.Diagnostics.Debugger.Log 文档,该方法具有基于在 Visual Studio 中选择的设置的奇怪行为。如果启用了非托管代码调试,Debugger.Log 输出的字符串会附加一个行分隔符。否则,所有字符串都写在同一行。

    您可能想尝试System.Diagnostics.Debug 类的各种方法。有Write 方法(接受换行符)和WriteLine 方法自动附加换行符。两者都有接受格式化字​​符串的重载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      相关资源
      最近更新 更多