Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save our actions on a console window into a text file?

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.

like image 871
kiss my armpit Avatar asked May 24 '12 18:05

kiss my armpit


3 Answers

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.

like image 78
Servy Avatar answered Nov 20 '22 11:11

Servy


There is nothing that will automatically "shadow" your console IO, so you'll have to do this yourself. That being said, it's fairly easy to open a text file and write anything you want to it.

In your case, just open a text file, and write the information to it as you write to the console and read from the console, as needed. For details, see MSDN's How to: Write To a Text File.

like image 42
Reed Copsey Avatar answered Nov 20 '22 12:11

Reed Copsey


You could use a StreamWriter to output what you capture via the Console:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ProgramOutput.txt", true))
        {
            file.WriteLine(name);
        }  
like image 1
Darren Avatar answered Nov 20 '22 12:11

Darren