This post originated from an RSS feed registered with .NET Buzz
by Jeff Key.
Original Post: NullTextWriter
Feed Title: Jeff Key
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/jkey/Rss.aspx
Feed Description: Topics revolve around .NET and the Windows platform.
I'm trying to do some profiling on a command line app that uses a number of assemblies that spit out a bunch of debug information via Console.WriteLine (this isn't the intent and the calls have since been removed; I just don't have the latest bits yet). This is no problem on the command line, as I can redirect output to null via “> nul”. Unfortunately, this doesn't work when started from a profiler.
The simple solution was to add an optional “-null” argument to the application. When that argument is given, I replace the standard out with a NullTextWriter, then re-add the standard out at program end in case any exceptions were thrown, etc. Here's the magic:
public class NullTextWriter : TextWriter { public override System.Text.Encoding Encoding { get { return ASCIIEncoding.Default; } } }
You can set the output stream by calling Console.SetOut(TextWriter). Works for me.