none
How To Redirect Console Output to a File

    General discussion

  • I often need to test functions which do not return or alter any data, but write output to the console. In order to automate testing, I redirect the console output to a file, call the function, then verify the output file for correctness. To ease this process, I create a simple class called OutToFile. Using OutToFile is easy and intuitive:

                // Output to the console by default
                Console.WriteLine("\nTryOutToFileClass()");
                Console.WriteLine("Redirecting output to file.txt");
    
                // Console.Write and Console.WriteLine's output 
                // will go into a file
                using(OutToFile redir = new OutToFile("file.txt"))
                {
                    Console.WriteLine("text in file.txt");
                }
    
                // Output return to the console
                Console.WriteLine("\nCheck the file file.txt for captured output");
                Console.WriteLine("Press any key...");
                Console.ReadKey();
    
     
    Here is the source code for OutToFile:
     
    // Redirection Utility
    // Author: Hai Vu (haivu2004 on Google mail)
    
    using System;
    using System.IO;
    
    namespace RedirectIO
    {
        /// <summary>
        /// OutToFile is an easy way to redirect console output to a file.
        /// 
        /// Usage:
        ///     Console.WriteLine("This text goes to the console by default");
        ///     using (OutToFile redir = new OutToFile("out.txt"))
        ///     {
        ///         Console.WriteLine("Contents of out.txt");
        ///     }
        ///     Console.WriteLine("This text goes to console again");
        /// 
        /// </summary>
        public class OutToFile : IDisposable
        {
            private StreamWriter fileOutput;
            private TextWriter oldOutput;
    
            /// <summary>
            /// Create a new object to redirect the output
            /// </summary>
            /// <param name="outFileName">
            /// The name of the file to capture console output
            /// </param>
            public OutToFile(string outFileName)
            {
                oldOutput = Console.Out;
                fileOutput = new StreamWriter(
                    new FileStream(outFileName, FileMode.Create)
                    );
                fileOutput.AutoFlush = true;
                Console.SetOut(fileOutput);
            }
    
            // Dispose() is called automatically when the object 
            // goes out of scope
            public void Dispose()
            {
                Console.SetOut(oldOutput);  // Restore the console output
                fileOutput.Close();         // Done with the file
            }
        }
    }
    
    Saturday, January 05, 2008 3:25 AM

All replies

  • Hi,

    Can we use this code for web application where we can show the output to the website client and the code runs on the web server?

    Regards,
    Adarsh...
    Tuesday, February 12, 2008 3:12 PM