How to save our actions in the console window to 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 these actions are performed in the 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 these actions in a text file so that I can use it as a log?

Edit:

I have to explain the real scenario. I am writing a book about C #. I want to avoid attaching the screen to 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 contents of the console window in the book. Adding additional code (to create a text file) will complicate the code example, which in turn will make the reader confused.

+6
source share
4 answers

Ok, so the idea is to change the reader / writer for the input and output of the console to do what they did before, but also write to the log file. I created a log class that should probably be expanded to take the file name / path as a parameter, but it is important to have it so that the output stream can 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 have created these classes, we will do the following at the beginning of Main :

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

We will also need this at the end of Main :

 log.Close(); 

This is a somewhat quick and dirty drink. If you use this in production, you probably want to change a lot of class / method names.

+2
source

There is nothing that automatically obscures your I / O console, so you have to do it yourself. It’s pretty easy to open a text file and write whatever you want.

In your case, just open a text file and write the information on it when you write to the console and read from the console, as necessary. For more information, see MSDN How to write to a text file .

+1
source

You can use StreamWriter to output what you captured using Console :

 using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ProgramOutput.txt", true)) { file.WriteLine(name); } 
+1
source

This example will help 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(); 

More details: 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

0
source

Source: https://habr.com/ru/post/916590/


All Articles