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

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -