There is a lot of text printed on the terminal window from my C# code, some text is printed by an other app that I ran through C# System.Diagnostics (without RedirectStandardOutput neither I wanna use that async thing) and it printed it's own text and some text is printed by C# Console.WriteLine function.
I want to save all of that text from top to bottom to a text file. I don't want to execute any file and store it's text since all the execution is already done and all the text is printed already. I just want to save all of that text to a file at the end of the program.
NOTE: The following is not my real code (obviously) but it looks kind of like this.
from rich.progress import track
import time, os
print("Python Test")
for i in track(range(20), description="Processing..."):
time.sleep(0.1) # Simulate work being done
os.system("color 08")
using System.Diagnostics;
Console.WriteLine("Test");
// Create a new process instance
Process process = new Process();
// Configure the process using StartInfo
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/c echo Hello world! & timeout /t 2 & python test.py";
// Start the process
process.Start();
// Wait for CMD to finish
process.WaitForExit();
Console.WriteLine("Test1");
/*
---- Save all of that above text that was printed here at the end of the code. ----
*/
I don't want to use RedirectStandardOutput because as far as I know if I do that and print the things from the process using those async things, first, it will not be able to print live updates such as in that timeout /t 2 part, it's also won't be able to account for change in color of the window color 08.
I want such a system that will let the code execute normally but when the code reaches the end it exports the text to a file much like how the now Windows Terminal's Export Text feature works.
This is what I want to achieve:
The terminal should work normally no change in how it works.

After the all the things are done at the end of my C# code I want to save all of that text to a text file.

Definition of STDIN, STDOUT, and STDERR
The OS' kernel of all operating systems use these 3 main I/O (Input/Output) streams, and these are STDIN, STDOUT, and STDERR. STDIN is the I/O stream that is processing information related to input, STDOUT is the I/O stream that is processing information related to output, and STDERR is the I/O stream that is processing information related to errors. All services, applications, and components of the OS' kernel use these streams to manipulate I/O information on any OS.
Solution using STDOUT redirection
In the code provided by you, the issue defined is that you want to process the information provided by the operation of a Batch script without redirecting the STDOUT stream. The ways in which the C# application can read the output of the Batch script is either by using files (e.g. JSON configuration files), Sockets, Pipes, and the STDOUT stream. The aforementioned methods are Inter-Process communication methods ( You can check this link for more information: https://stackoverflow.com/a/76196178/16587692 ). These methods must be used because there is no direct communication channel between the C# application and the Batch script. In this situation the most viable method is to use the STDOUT stream. You can preserve the console colors by changing them through the C# application rather than changing them by passing arguments to the Batch script. You can also write the output in real time to the desired log file, but as a word of caution, do not read from the log file while the C# application is writing to that file because it will trigger a Race Condition and this may corrupt your HDD/SSD cells where the file is located.
STDOUT redirection method output
Definition of FIFO Pipes
FIFO Pipes, also known as Named Pipes, are a type of socket that use the operating system's file system in order to facilitate information exchange between applications. FIFO Pipes are categorised into 3 categories and these are, Inward Pipes, Outward Pipes, and Two-way Pipes. Inward Pipes are pipes on which the Pipe Server can only receive information from the Pipe Clients, Outward Pipes are pipes on which the Pipe Server can only send information to the Pipe Clients, and Two-way Pipes are pipes on which the Pipe Server can both send and receive information to and from the Pipe Clients.
Cross-platform solution using FIFO Pipes
If the integrity of the output must be preserved, FIFO pipes are the best option for an inter-process communication method. In this scenario an Inward Pipe will be the best solution due to the fact that the C# application must receive information from the Python application. In order to have cross-platform capabilities, both the C# and Python application use conditional statements to verify which OS platform the applications are running on.
C# Application
Python Application
FIFO Pipes output Windows
FIFO Pipes output Linux
Linux OS Details
Linux OS Output