c# - How to save the output of a console application -
i need advice on how have c# console application display text user through standard output while still being able access later on. actual feature implement dump entire output buffer text file @ end of program execution.
the workaround use while don't find cleaner approach subclass textwriter
overriding writing methods both write file , call original stdout writer. this:
public class dirtyworkaround { private class dirtywriter : textwriter { private textwriter stdoutwriter; private streamwriter filewriter; public dirtywriter(string path, textwriter stdoutwriter) { this.stdoutwriter = stdoutwriter; this.filewriter = new streamwriter(path); } override public void write(string s) { stdoutwriter.write(s); filewriter.write(s); filewriter.flush(); } // same above writeline() , writeline(string), // plus whatever methods need override inherit // textwriter (encoding.get guess). } public static void main(string[] args) { using (dirtywriter dw = new dirtywriter("path", console.out)) { console.setout(dw); // teh codez } } }
see writes , flushes file time. i'd love @ end of execution, couldn't find way access output buffer.
also, excuse inaccuracies above code (had write ad hoc, sorry ;).
the perfect solution use log4net console appender , file appender. there many other appenders available well. allows turn different appenders off , on @ runtime.
Comments
Post a Comment