views:

114

answers:

6

Possible Duplicate:
Mirroring console output to a file

I've a console application in C# and I want to Log into a text file every text appears on the screen. How to do that ?

Unfortunately this thread is closed despite of my comment about the Mirroring console output to a file. The accepted post as answer by that thread IS NOT CORRECT. So why cannot ask the question again? I'm using console interactively and want a copy of all the text on console in a text file. SOS

A: 

A simple Google search result : here

Sorry I am no longer programming with C# but I checked to code and it does make sense in this approach :)

Michael Mao
This will redirect output, not copy it. You get either console output or file output. See the linked question to see how to mirror the output.
Michael Petrotta
Then how about a StringBuilder with a using block at the end of the program?
Crag
+I searched google but all was about how to log into console not how to log console into something :) So the right question is what were the keywords ?
Xaqron
+1  A: 

Replace all references to the Console class with the System.Diagnostics.Trace class. You can add a ConsoleTraceListener and a TextWriterTraceListener, and then everything you output to the console will end up in both places.

Joel Coehoorn
It works but what if I don't want to replace my Console.Write() with anything else. Is there a solution for it without losing the screen ?
Xaqron
Set up the trace class to also write to the screen.
Joel Coehoorn
+1  A: 

You could use Console.SetOut to redirect the output to a file, but then you wouldn't get output to the console as well unless you used a TextWriter that streamed to both.

You could try using System.Diagnostics.Trace and then adding listeners to both the console and a file.

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="configConsoleListener"
             type="System.Diagnostics.ConsoleTraceListener" />
        <add name="fileLogger" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="LogFile.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
 </configuration>
Daniel Ballinger
I prefer to have redundancy in my code rather than dealing with config file if it's the best choice.
Xaqron
+1  A: 
C:\>yourconsoleapp.exe > yourtextfile.txt

edit: This assumes your console app requires no user input.

Kyle B.
called redirection - you can also pipe if you want to input something: C:\>yourconsoleapp.exe | "some input" > yourtextfile.txt. More info here: http://technet.microsoft.com/en-us/library/bb490982.aspx
Mrk Mnl
A: 

This may be overly simplistic, but at a command prompt, if you do:

C:\>MyApplication >> output.txt

That will write all output from that given app to output.txt

If you don't actually need a programmatic solution, this might do the trick.

CubanX
A: 

Use log4j.

  1. Go to log4j website and download dll.
  2. Reference dll in your project.
  3. Add this to top of your app.config
<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] -%message%newline" />
    </layout>
</appender>


<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] -%message%newline" />
    </layout>
</appender>
  1. Put this as a member of any class you want to log to:

    private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

  2. Put in a line like this to log:

log.Debug("Hello world!");

LWoodyiii
Thanks for your time. I compensated one negative but cannot accept it as my answer.
Xaqron