title: Setting the color for Console.Error writes
body: <p>I’m writing a console application that has a custom logger in it. The logger needs to color anything sent to Console.Error in red. I now have third party references that also write to Console.Out and Console.Error so I have to do this in a way that accounts for them too since I don’t have their source code. I’ve setup my logger so that any code writing to the Console will be using my logger’s TextWriter’s by calling the Console.SetOut &amp; Console.SetError methods. Its kinda-of working but I’m guessing there is some sort of synchronization issue? In my ErrorWriter class you can see I’m setting the console’s foreground color and then calling base.Write() but the result is not as expected. Text coming from Console.Out that should be gray is coming out red and mid-stream it suddenly turns to gray. It’s been consistently changing color from red back to gray at the same character position but I’m assuming that base.Write() doesn’t actually spit out to the Console immediately; there is some kind of lag time/buffer. I’ve tried calling base.Flush() but that makes ALL of the text from Console.Out red which is even worse. How do I solve this problem?</p>
  
  <pre><code>public class Logger
  {
      private static TextWriter _out;
      private static ErrorWriter _error;
  
      public Logger()
      {
          Initiliaze();
      }
  
      public static TextWriter Out
      {
          get
          {
              return _out;
          }
      }
  
      public static TextWriter Error
      {
          get
          {
              return _error;
          }
      }
  
      private static void Initiliaze()
      {
          if (_out == null)
              _out = new StreamWriter(Console.OpenStandardOutput());
          if (_error == null)
              _error = new ErrorWriter(Console.OpenStandardError());
  
          Console.SetOut(Out);
          Console.SetOut(Error);
      }
  }
  
  public class ErrorWriter : StreamWriter
  {
      // constructors omitted to save space
  
      public override void Write(string value)
      {
              Console.ForegroundColor = ConsoleColor.Red;
              base.Write(value);
              //base.Flush();
              Console.ResetColor();
      }
  
      public override Encoding Encoding
      {
          get { return Encoding.Default; }
      }
  }
  </code></pre>