How to Redirect Console Output to a File in C# «...

Transcription

How to Redirect Console Output to a File in C# «...
How to Redirect Console Output to a File in C# « Hai’s Blog
1 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
RSS Subscribe: RSS feed
Hai’s Blog
Useful stuff, life, photography, kids, technical, programming, and what-else?
How to Redirect Console Output to a File in C#
Posted on January 5, 2008 by Hai
5
I encountered the need to redirect the console output to a file in my attempt to automate testing of functions
which does not return any value but writes to the console. Basically, we need to save the old output
(Console.Out) before assigning the output to a file stream by means of Console.SetOut(). After the test, I
also need to restore the console output.
After doing this for a while, it gets old and I told myself, “Self, I can create a simple class to simplify this
task.” In my vision, output redirect should be easy, simple, and practical. The user should not have to
remember the details such as which data type to use (TextWriter, FileStream, StreamWriter, …), or which
steps to take. Thus, I created OutToFile, a simple class which encapsulate this idea. Using OutToFile is … you
guessed it, easy:
The source code for this class is available, you are free to use it, but I reserve the bragging right
.
Blogged with Flock
Tags: .net, C#, CSharp
2011-10-07 02:04
How to Redirect Console Output to a File in C# « Hai’s Blog
2 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
Share this:
Like this:
Be the first to like this post.
Posted in: Programming, Windows
← .Net Exceptions List
Find Largest Sum of Contiguous Numbers in an Array →
5 Responses “How to Redirect Console Output to a File in C#” →
1.
Eliot
December 15, 2009
This is exactly what I was looking for! We have tons of console print debugging statements in our XNA
game engine: http://pantherengine.sourceforge.net/ With this class I believe we can catch all of them
and redirect them to a file.
Thanks for allowing us to use this code. If we wind up using it it will be noted that it is not under the
project’s Apache 2.0 license.
2.
Edgar
September 17, 2010
Hello Hai,
Do you know how I may be able to output this process to a txt or log file?
Process ps = new Process();
ps = Process.Start(NUNIT_CONSOLE, path + “\\” + nunitFile + ” /include:\”" + sTestConcepts + “\”"
+ “/out:C:/Users/Edgar/Documents/TestResults.txt” );
//wait until the process is done
while (!ps.HasExited) System.Threading.Thread.Sleep(100);
testConcepts = new ArrayList();
Thank you in advance for your help!
-Edgar
3.
Hai
September 18, 2010
Edgar,
2011-10-07 02:04
How to Redirect Console Output to a File in C# « Hai’s Blog
3 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
See if something like this can help:
http://channel9.msdn.com/forums/Coffeehouse/250664-C-Execute-External-App-and-capture-output/
If not, google for C# process capture output and you will probably find something that works. Cheers.
4.
Edgar
September 20, 2010
Hai,
Thank you for your help and quick response! The link you provided was very helpful. I ended up using
a stream writer and it works now. I am having trouble with the console freezing while running (it is
running multiple processes) but this is a different issue.
I appreciate the help!
Cheers
1 Trackback For This Post
1. Problems in C# and Solutions | Radin Ahmed Ehsan →
July 10th, 2011 → 2:44 pm
[...] go to this link to find out the details. GA_googleAddAttr("AdOpt", "1");
GA_googleAddAttr("Origin", "other"); [...]
2011-10-07 02:04
How to Redirect Console Output to a File in C# « Hai’s Blog
4 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
Leave a Reply
Log In
Log In
Notify me of follow-up comments via email.
Log In
Post Comment
Notify me of new posts via email.
About
Email Subscription
Enter your email address to subscribe to this blog and receive notifications of new posts by email.
Join 13 other followers
Categories
2011-10-07 02:04
How to Redirect Console Output to a File in C# « Hai’s Blog
5 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
Admin (2)
Cool (24)
Family (4)
Finance (1)
Gadget (6)
Humor (2)
Internet (25)
Interview (7)
IOS (2)
Journal (10)
Misc (1)
Opinion (7)
OSX (53)
Photography (16)
Programming (71)
bash (7)
Python (2)
Tcl (43)
Review (30)
Tips & Tricks (63)
Uncategorized (4)
Unix (15)
Video (4)
Windows (29)
Archives
September 2011 (1)
August 2011 (3)
July 2011 (3)
April 2011 (8)
March 2011 (9)
February 2011 (4)
January 2011 (12)
December 2010 (1)
June 2010 (1)
May 2010 (1)
April 2010 (1)
March 2010 (2)
January 2010 (1)
October 2009 (3)
September 2009 (7)
August 2009 (2)
July 2009 (1)
May 2009 (7)
April 2009 (8)
January 2009 (1)
December 2008 (8)
November 2008 (1)
September 2008 (7)
August 2008 (2)
2011-10-07 02:04
How to Redirect Console Output to a File in C# « Hai’s Blog
6 of 6
http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-t...
July 2008 (2)
April 2008 (1)
March 2008 (1)
February 2008 (1)
January 2008 (9)
December 2007 (9)
November 2007 (6)
September 2007 (2)
August 2007 (2)
July 2007 (4)
June 2007 (2)
May 2007 (2)
April 2007 (1)
March 2007 (3)
February 2007 (3)
January 2007 (6)
December 2006 (9)
September 2006 (5)
August 2006 (6)
June 2006 (4)
April 2006 (2)
March 2006 (1)
February 2006 (10)
November 2005 (3)
October 2005 (5)
September 2005 (4)
July 2005 (1)
June 2005 (2)
May 2005 (1)
April 2005 (1)
February 2005 (2)
December 2004 (1)
Blog at WordPress.com.
Theme: Inuit Types by BizzArtic.
2011-10-07 02:04