Tutorial :Visual Studio: Who is writing to console?



Question:

OK, here's a good one (I think) - I'm working on an application with lots (far too many) dependency dlls, created by a team of developers. I'm trying to debug just one assembly, but the console output is 'polluted' by the Console.WriteLines and Debug.WriteLines left scattered around the code.

Is there anyway I can work out exactly which assembly a given line is coming from, so I can get the author to clean up their source?

UPDATE If you're also experiencing this kind of issue, note that there is another potential source of output messages which is any breakpoints with 'When hit' set to print a message. Having said which, this is a VERY cool feature, which can prevent the kind of problems I was having above.


Solution:1

Yes - replace Console.Out. Use Console.SetOut after creating a TextWriter which not only dumps the requested data to the original console, but also dumps a stack trace (and timestamp, and the requested data) to a file.

Here's some code, adapted from Benjol's answer:

(Note: you will want to adapt this code depending on whether you want a stack trace after each write, or after each writeline. In the code below, each char is followed by a stack trace!)

using System.Diagnostics;  using System.IO;  using System.Text;    public sealed class StackTracingWriter : TextWriter  {      private readonly TextWriter writer;        public StackTracingWriter (string path)      {          writer = new StreamWriter(path) { AutoFlush = true };      }        public override System.Text.Encoding Encoding      {          get { return Encoding.UTF8; }      }        public override void Write(string value)      {          string trace = (new StackTrace(true)).ToString();          writer.Write(value + " - " + trace);      }        public override void Write(char[] buffer, int index, int count)      {          Write(new string(buffer, index, count));      }        public override void Write(char value)      {          // Note that this will create a stack trace for each character!          Write(value.ToString());      }        public override void WriteLine()      {          // This is almost always going to be called in conjunction with          // real text, so don't bother writing a stack trace          writer.WriteLine();      }        protected override void Dispose(bool disposing)      {          writer.Dispose();      }  }  

To use this for logging both Console.WriteLine and Debug.WriteLine to a file, make calls like this as early as possible in your code:

var writer = new StackTracingWriter(@"C:\Temp\ConsoleOut.txt");  Console.SetOut(writer);  Debug.Listeners.Add(new TextWriterTraceListener(writer));  

Note that this currently doesn't also write to the original console. To do so, you'd need to have a second TextWriter (for the original console) in StackTracingWriter, and write to both places each time. Debug will however continue to be written to the original console.


Solution:2

Download Reflector and you can open up the mscorlib assembly, add your application's assemblies, then right click on the Console class and click Analyze and you can show all methods that reference the Console class.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »