5

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

Community
  • 1
  • 1
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • Funny thing about that question is that the accepted answer is just another "how to write a text to console". I tried it before asking this question. It has added two listeners (Text & Console) and when he writes to Trace it appears in console and text file. – Xaqron Oct 08 '10 at 00:47
  • @Xaqron: is that not what you want? What exactly are you looking for? – Michael Petrotta Oct 08 '10 at 00:58
  • It's what I want (as title of question says) but the answer is about how to write to a text file and console at the same time and it is accepted by question owner! I want to bind a text file to console regardless of who is writing to console. In fact I'm using console for debugging a multi-threaded application but the tests takes hours and I don't want to lose the console texts. I'm not gonna implement logging since I just have what I want on the screen. I want to have the screen so output redirection ( >> ) is not my choice. – Xaqron Oct 08 '10 at 01:30
  • So, you mean you have no ability to modify the console app at all? – Michael Petrotta Oct 08 '10 at 02:48
  • @Xaqron - that's not the way Stack Overflow works. An "accepted answer" means only that the answer worked for the OP, not that it's necessarily the right answer. If you think that it's wrong, and want to encourage more attention, why not attach a [bounty](http://stackoverflow.com/faq#bounty) to the other question? – Michael Petrotta Apr 11 '11 at 01:34

6 Answers6

2
C:\>yourconsoleapp.exe > yourtextfile.txt

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

Kyle B.
  • 5,737
  • 6
  • 39
  • 57
  • 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 – markmnl Oct 26 '10 at 11:20
1

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
  • 399,467
  • 113
  • 570
  • 794
1

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
  • 13,187
  • 11
  • 69
  • 96
  • I prefer to have redundancy in my code rather than dealing with config file if it's the best choice. – Xaqron Oct 08 '10 at 00:54
1

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
  • 5,176
  • 2
  • 29
  • 44
0

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
  • 9,878
  • 23
  • 75
  • 91
  • 1
    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 Oct 08 '10 at 00:35
  • Then how about a StringBuilder with a using block at the end of the program? – Crag Oct 08 '10 at 00:37
  • +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 Oct 08 '10 at 00:41
0

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!");

LeWoody
  • 3,593
  • 4
  • 25
  • 31