使用 System.Diagnostics.Debugger.Log 打印换行符

发布于 2024-11-19 12:00:14 字数 332 浏览 1 评论 0原文

我在调试时在日志中打印很多行,如下所示:

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

所有这些行都在同一行中打印。我怎样才能使它们在单独的行中打印?

我尝试使用

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

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

I am printing a lot of lines in my log while debugging like this:

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

All of them are getting printed at the same line.. How can i make them print in seperate lines?

I tried using

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

But, it didnt work. Any help will be appreciated . Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2024-11-26 12:00:14

在 .NET 中,惯用语 Environment.NewLine 是一个 System.String,它由正确的 System.Char 组成,用于终止一行文本,所以:

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 呢?

我只会向您指出这里的 stackoverflow 问题,但要点如下。

您可以在 App.config 中进行设置,但是,当然,您也可以仅以编程方式创建它,因为 app.config 部分只是创建对象!

类似:

     ⋮
   <trace>
      <!-- note: notional notation only -->
      <add name="consoleLog" logLevel="debug" enabled="" type="⋯ 
      <add name="netLog" logLevel="verbose" enabled="false" addr="rdp://127.0.0.1:1935/nothing/stream" type="⋯
      <add name="fileLog" logLevel="errors" enabled="true" file="c:\boots.ini" type="⋯ 
   </trace>
     ⋮ 

然后您的代码调用 Trace() 就像调试()

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

是的,它是多目标的;您可以将其设置为多目标,并且可以使用内置的 System.Diagnostics Trace 类型(如 Console例如,如果您想打印到屏幕,则可以使用跟踪器),或者您可以根据需要创建自己的自定义类型。美丽的!

最后一句话:调试和跟踪都有很多辅助函数,可以使您所做的任何写入更具象征性; WriteLineIf、TraceError 等,在你弄清楚它们为什么存在之前,尝试一下它们是值得的。 几乎可以保证,您使用它们越多,您就会发现它们越有用。 ♡

In .NET the idiom Environment.NewLine is a System.String that consists of the proper System.Char(s) to terminate a line of text, so:

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

[Updated 2015-05-07]

Reflecting upon this a few years on, i feel like i dropped the ball on this at least a little bit (though, i do think that it's important to be able to do the low-level NewLine without having to fight the language sometimes as well; so i also like the original answer...)

First off, David Brown did give a good answer below: using System.Diagnostics.Debug.WriteLine (and Write) instead. That is a good solution to this, especially in the case of the OP, as the other parameters of the call aren't even really being used; and the Debug.Write/WriteLine calls looks like this (using for examples the OP's original calls, assuming for the sake of example that the OP's original first parameter responseFromServer was already terminated, and the second needed termination):

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

Easy peasy.

Better yet though, why not Trace?

I will just point you to this stackoverflow question here but here's the gist.

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!

Something like:

     ⋮
   <trace>
      <!-- note: notional notation only -->
      <add name="consoleLog" logLevel="debug" enabled="" type="⋯ 
      <add name="netLog" logLevel="verbose" enabled="false" addr="rdp://127.0.0.1:1935/nothing/stream" type="⋯
      <add name="fileLog" logLevel="errors" enabled="true" file="c:\boots.ini" type="⋯ 
   </trace>
     ⋮ 

and then your code calls Trace() just like Debug().

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

Yep, it's multi-target; and you can set it up to be multi-target, and you can either use built-in System.Diagnostics Trace types (like a Console tracer if you want to print to the screen, for example) or you can create your own custom types as necessary. Beautiful!

Last word: both Debug and Trace have lots of helper functions that are there to make whatever writes you're doing more symbolic; WriteLineIf, TraceError, etc. and it pays to play around with them until you figure out why they are there. It's almost guaranteed that the more you use them, the more useful you will find them. ♡

一场信仰旅途 2024-11-26 12:00:14

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

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

According to the System.Diagnostics.Debugger.Log documentation, the method has a strange behavior based on settings chosen in Visual Studio. If unmanaged code debugging is enabled, strings output by Debugger.Log have a line separator appended. Otherwise, all strings are written on the same line.

You might want to try the various methods of the System.Diagnostics.Debug class instead. There are Write methods (that accept newlines) and WriteLine methods that append the newline automatically. Both of which also have overloads that accept formatted strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文