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 6637559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:48+00:00 2026-05-25T23:19:48+00:00

I wonder what the best approach is in implementing a logger which essentially eats

  • 0

I wonder what the best approach is in implementing a logger which essentially eats all Console.WriteLine() messages and spits out a text file of all these messages.

I know I could use a StringBuilder and populate it with all the messages by doing .AppendLine wherever Console.WriteLine occurs…but I am looking for something more magical than that. Some way using which I don’t have to write as many lines of code as Console.WriteLines in existing code.

And in case you missed the tags, this is for a C#4.0 console application.

  • 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-05-25T23:19:49+00:00Added an answer on May 25, 2026 at 11:19 pm

    I assume what you want is a stream that writes to standard out and to a file. That is, you want everything to still show up on the console, but you also want it to be written to a file. (If you just wanted to redirect output to a file, you could do that from the command line.)

    You need to create your own TextWriter-derived class, and have it do the actual output. You call Console.SetOut to make the console write to your TextWriter.

    class ConsoleLogger: TextWriter
    {
        private TextWriter ConsoleOutputStream;
        private StreamWriter LogOutputStream;
        public ConsoleLogger(string logFilename)
        {
            ConsoleOutputStream = Console.Out;
            // Initialize your log file
            LogOutputStream = new StreamWriter(logFilename);
        }
    
        public void Write(string s)
        {
            ConsoleOutputStream.Write(s);
            LogOutputStream.Write(s);
        }
    
        // Other Write functions format their arguments and call Write(string).
    }
    

    And in the program that uses this:

    private ConsoleLogger MyLogger;
    const string LogFilename = "ConsoleLog.txt";
    void Main()
    {
        MyLogger = new ConsoleLogger(LogFilename);
        Console.SetOut(MyLogger);
    }
    

    You don’t have to override all the TextWriter methods. As the documentation says, “A derived class must minimally implement the TextWriter.Write(Char) method to make a useful instance of TextWriter.”

    I’ve done something similar to this, and it works quite well.

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

Sidebar

Related Questions

I wonder what the best approach is for a situation where I want to
I just wonder what the best approach is to have multiple users work on
I wonder if anyone could suggest the best way of looping through all the
I wonder what's the best approach for using third party js libraries on a
I wonder what's the best approach to combine several CCSprites dynamically into one grouped
I wonder what is the best approach to make a JOptionPane style plain message
I wonder if anyone can suggest the best approach to detect collision between multiple
Which is the best control/approach in ASP.NET 4 to bind data from single record,
I wonder what the best way to make an entire tr clickable would be?
I'm wonder what the best way to convert a byte array (length 4) to

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.