I am sending stats to a console window while running a console application. I would like to periodically flush the contents of the console to a text file and also when and if the program closes. I thought console.out.writeline() would work but it does not produce a value. I cannot keep track of everything that gets written to the console window, so basically after every 6 hours I would like to just write everything that is displayed at that time to a text file. Just want to know if this is even possible. Thanks in advance.
-
1possible duplicate of [Is there a way to continuously mirror the result of Console.Write to a collection (array,list,etc)?](http://stackoverflow.com/questions/5368229/is-there-a-way-to-continuously-mirror-the-result-of-console-write-to-a-collectio) – Hans Passant Sep 16 '13 at 13:31
1 Answers
All things considered, you're better off using something that will log to the console and to a file. So instead of calling Console.WriteLine
, you call Logger.WriteLine
, and it writes to both.
You can read from the console and write to a file, but it's a little difficult and it's not perfect. The console screen buffer can only hold so much. If you're outputting a lot of data, some of it will be pushed out of the buffer.
You can set the size of the screen buffer by changing the properties of the console window.
If you want to read data from the console, you have to read the console screen buffer. The .NET Console
class doesn't provide access to that. You end up having to call Windows API functions to do that.
I developed code some years back for a series of articles on using the Console API from .NET. Unfortunately, the site that published those articles is no longer online. But the code is available at http://www.mischel.com/pubs/consoledotnet.zip.
If you want to use that code to get the contents of the console, you would do the following:
Get the active console screen buffer by calling JConsole.GetActiveScreenBuffer
Call GetScreenBufferInfo to get the size of the screen buffer
Call ReadBlock to read as much of the buffer as you want
That will return you a two-dimensional array of ConsoleCharInfo
structures. You can then go through that and get the characters you need.
Note that this won't necessarily get everything that's been output to the console. Rather, it will get everything that's currently in the screen buffer. If the application wrote more than will fit in the screen buffer, you will not get the information that scrolled off.

- 131,090
- 20
- 188
- 351
-
Thank you. Very informative. I have considered the option of logging in parallel which I typically do but in this case I was seeing an issue with the timer I created which after 6 hours should write to a text log file. So I thought since the console window displays the data in set format and cursorposition, I could just 'flush' at any give point. But as you point out not that easy. Thanks. Will use your sample code to test which would be better method. – vbNewbie Sep 16 '13 at 15:44
-
@vbNewbie: I would strongly encourage you to use the technique that Hans Passant linked to in his comment. – Jim Mischel Sep 16 '13 at 15:56
-
that is a good option but I think in my regard I don't want to log each time the console output is made which is every second. I only want to log to file every 6 hours which I have done now with a timer and keeping track of stats variables. – vbNewbie Sep 16 '13 at 17:12