Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing newline using System.Diagnostics.Debugger.Log

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

like image 273
CuriousCoder Avatar asked Jul 10 '11 21:07

CuriousCoder


2 Answers

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. ♡

like image 78
shelleybutterfly Avatar answered Sep 22 '22 14:09

shelleybutterfly


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.

like image 24
David Brown Avatar answered Sep 23 '22 14:09

David Brown