使用 System.Diagnostics.Debugger.Log 打印换行符
我在调试时在日志中打印很多行,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 中,惯用语
Environment.NewLine
是一个System.String
,它由正确的System.Char
组成,用于终止一行文本,所以:[更新于 2015-05-07]
几年后反思这一点,我觉得我在这方面至少有一点失误有点(虽然,我确实认为能够在不与语言作斗争的情况下完成低级的 NewLine 是很重要的,所以我也喜欢原来的答案......)
首先,David Brown 确实给出了下面是一个很好的答案:使用 System.Diagnostics.Debug.WriteLine (和 Write)代替。这是一个很好的解决方案,特别是在 OP 的情况下,因为调用的其他参数甚至没有真正被使用;并且
Debug.Write
/WriteLine
调用如下所示(例如使用 OP 的原始调用,假设 OP 的原始第一个参数responseFromServer
已经终止,第二个需要终止):简单。
更好的是,为什么不
Trace
呢?我只会向您指出这里的 stackoverflow 问题,但要点如下。
您可以在 App.config 中进行设置,但是,当然,您也可以仅以编程方式创建它,因为 app.config 部分只是创建对象!
类似:
然后您的代码调用
Trace()
就像调试()
。是的,它是多目标的;您可以将其设置为多目标,并且可以使用内置的 System.Diagnostics
Trace
类型(如Console
例如,如果您想打印到屏幕,则可以使用跟踪器),或者您可以根据需要创建自己的自定义类型。美丽的!最后一句话:调试和跟踪都有很多辅助函数,可以使您所做的任何写入更具象征性; WriteLineIf、TraceError 等,在你弄清楚它们为什么存在之前,尝试一下它们是值得的。 几乎可以保证,您使用它们越多,您就会发现它们越有用。 ♡
In .NET the idiom
Environment.NewLine
is aSystem.String
that consists of the properSystem.Char
(s) to terminate a line of text, so:[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
(andWrite
) 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 theDebug.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 parameterresponseFromServer
was already terminated, and the second needed termination):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:
and then your code calls
Trace()
just likeDebug()
.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 aConsole
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. ♡
根据
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 byDebugger.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 areWrite
methods (that accept newlines) andWriteLine
methods that append the newline automatically. Both of which also have overloads that accept formatted strings.