Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:39:56+00:00 2026-06-03T16:39:56+00:00

I’m writing a console application that has a custom logger in it. The logger

  • 0

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 & 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?

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; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:39:57+00:00Added an answer on June 3, 2026 at 4:39 pm

    Turned out to be a stupid copy/paste error! I had two calls to Console.SetOut(). Now its working after change the second one to Console.SetError()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a console application under Ubuntu that uses the serial port. It needs
So I am writing a C# console application. I have a text file that
I'm writing a sample console application in VS2008. Now I have a Console.WriteLine() method
I'm writing a console application that will be deployed with an installation of an
I am writing a console application that will do two tasks. The app will
I am writing a console application in python that will consist of a handful
I have been writing C++ Console/CMD-line applications for about a year now and would
I was writing a console application that would try to guess a number by
I have a console application that sends customized emails (with attachments) to different recipients
I have an application that needs to retrieve a value out of a hidden

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.