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

  • Home
  • SEARCH
  • 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 7984061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:08:54+00:00 2026-06-04T11:08:54+00:00

If we compile the following code and run the resulting application, we will interact

  • 0

If we compile the following code and run the resulting application, we will interact with it by entering our name, pressing enter, and pressing any key to exit. All of these actions are done on a console window.

using System;

namespace HelloWorld
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.Write("Enter your name: ");
            var name = Console.ReadLine();
            Console.WriteLine("Your name is {0} ", name);
            Console.ReadKey();

        }
    }
}

Is it possible to save all of these actions to a text file such that I can make use of it as a log?

Edit:

I should explain the real scenario. I am writing a book about C#. I want to avoid attaching the screen shot of the console window to the book because I want to make the file size of my book as small as possible. Instead, I want to attach a text file showing the content of console window to the book. Adding additional code (for creating the text file) will complicate the code example, which in turn will make the reader get confused.

  • 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-04T11:08:55+00:00Added an answer on June 4, 2026 at 11:08 am

    Okay, so the idea here is to change the reader/writer for console input and output to do what they did before but also write out to a log file. I made a Log class which should probably be extended so as to take the filename/path as a parameter, but it was important to have one so that the output stream could be synchronized.

    public class Log
    {
        private StreamWriter output;
        public Log()
        {
            output = new StreamWriter(File.OpenWrite("output.txt"));
    
        }
        public void Write(char c)
        {
            lock (output)
            {
                output.Write(c);
            }
        }
        public void Close()
        {
            output.Close();
        }
    }
    
    public class MyConsoleOutput : TextWriter
    {
        private TextWriter standard;
        private Log log;
    
        public MyConsoleOutput(TextWriter standard, Log log)
        {
            this.standard = standard;
            this.log = log;
        }
    
        public override void Write(char value)
        {
            standard.Write(value);
            log.Write(value);
        }
    
        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }
    
        protected override void Dispose(bool disposing)
        {
            standard.Dispose();
        }
    }
    
    public class MyConsoleInput : TextReader
    {
        private TextReader standard;
        private Log log;
        public MyConsoleInput(TextReader standard, Log log)
        {
            this.standard = standard;
            this.log = log;
        }
    
        public override int Peek()
        {
            return standard.Peek();
        }
    
    
        public override int Read()
        {
            int result = standard.Read();
            log.Write((char)result);
            return result;
        }
        protected override void Dispose(bool disposing)
        {
            standard.Dispose();
        }
    }
    

    Now that we’ve created these classes we’ll do the following right at the start of Main:

    Log log = new Log();
    Console.SetOut(new MyConsoleOutput(Console.Out, log));
    Console.SetIn(new MyConsoleInput(Console.In, log));
    

    We’ll also need this at the end of Main:

    log.Close();
    

    It’s a somewhat quick and dirty write up. If you use this in production you’ll likely want to change a lot of the class/method names.

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

Sidebar

Related Questions

Following code will compile but crash at run time: int main() { try {
When I compile the following code, I only see the error during Run-time which
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
I have following code snippet that i use to compile class at the run
Please help me to understand why the following code will not compile: #include <stdio.h>
The following code compile without errors. Basically, the C#2005 Console application calls VC++2005 class
If one were to compile and run the following code, one would find that
The following code using boost::asio will not compile: #ifndef _SERVER_H_ #define _SERVER_H_ #include Connection.h
I am not able to compile the following code. I run it in a
I'm trying to run the following code, but I'm getting a Type mismatch compile

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.