kiss my armpit
kiss my armpit

Reputation: 3519

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.

Upvotes: 6

Views: 1799

Answers (4)

gabba
gabba

Reputation: 2880

This example helps you:

Console.WriteLine("Hello World");
FileStream fs = new FileStream("Test.txt", FileMode.Create);
// First, save the standard output.
TextWriter tmp = Console.Out;
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.WriteLine("Hello file");
Console.SetOut(tmp);
Console.WriteLine("Hello World");
sw.Close();

Read more at: http://msdn.microsoft.com/ru-ru/library/system.console.setin.aspx, http://msdn.microsoft.com/ru-ru/library/system.console.setout.aspx, http://msdn.microsoft.com/en-us/library/system.console.in.aspx, http://msdn.microsoft.com/ru-ru/library/system.console.out.aspx

Upvotes: 0

Servy
Servy

Reputation: 203821

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.

Upvotes: 2

Darren
Darren

Reputation: 70786

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);
        }  

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564851

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.

Upvotes: 1

Related Questions