0

I am attempting to follow this solution here to mirror my Console output to a log file as well, however, i noticed that the output to file gets cut off, so the full console output is not completely outputted in the file. Why is that? Is TextWriter limited to certain amount of lines?

private TextWriter txtMirror = new StreamWriter("mirror.txt");

// Write text
private void Log(string strText)
{
    Console.WriteLine(strText);
    txtMirror.WriteLine(strText);
}

p.s. the reason im using this solution is because I have Console.Writeline in functions as well that i call in the main(). so if i was to use this solution instead, i would have to open a using statement everywhere i have a Console.WriteLine()...which seems redundant

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • Could you explain little bit more about the issue? `cut off` means the text contents are not completely written to the file? Did you consider using logging framework such as log4net, NLog etc for writing logs to text files? – Chetan Nov 19 '20 at 06:41
  • 4
    Do you Flush / Close / Dispose your TextWriter? – Klaus Gütter Nov 19 '20 at 06:41
  • @ChetanRanpariya like for example if i have 100 statements/lines printed to the console, when i check the log file i see only about 60 statements/lines. i did consider log4net but for now im working on a simple console POC, so i'd rather not include 3rd party libraries/packages yet :) – Cataster Nov 19 '20 at 06:51
  • @KlausGütter youre right, i have to add these 2 lines after every `Log("...") txtMirror.Flush(); txtMirror.Close();`. but `using` seems to take care of this in much more elgant way :) – Cataster Nov 19 '20 at 07:13
  • You can add it also as last line in your Log method – Klaus Gütter Nov 19 '20 at 07:32

1 Answers1

1

You can use the AutoFlush property System_IO_StreamWriter_Flush

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream after each write operation, but the encoder state will not be flushed.

By the way, if you are instantiating your logger class many times, you are going to have many StreamWriter objects. Make sure you dispose them as per documentation

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

Disposing the objects, makes sure that flush is called and any buffered information is written to the underlying object.

Example:

// Write text
private void Log(string strText)
{
    Console.WriteLine(strText);
    using (StreamWriter txtMirror = new StreamWriter("mirror.txt")) {
        txtMirror.WriteLine(strText);
    }
}
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • oh so i have to add these 2 lines after every Log("...")? `txtMirror.Flush(); txtMirror.Close();` – Cataster Nov 19 '20 at 06:57
  • If you wrap the `StreamWriter` with a using statement, when the code exits the using block, it will call `Dispose`/`close` and `flush`, without you needing to do anything. – Athanasios Kataras Nov 19 '20 at 06:59
  • I added the code with using. It should not miss any lines if you use it that way. – Athanasios Kataras Nov 19 '20 at 07:01
  • oh but now im losing the append capability, so it overwrites everything and leaves just the last line – Cataster Nov 19 '20 at 07:06
  • what would be the equivalent of `FileMode.Append, FileAccess.Write` for StreamWriter? im trying something like this: `using (StreamWriter txtMirror = new StreamWriter(logFilePath, FileMode.Append, FileAccess.Write))` but this says `Cannot convert system.IO.FileMode to system.text.encoding` – Cataster Nov 19 '20 at 07:10
  • 1
    oh nvm its just `true`! now its appending :) `using (StreamWriter txtMirror = new StreamWriter("mirror.txt", true))` – Cataster Nov 19 '20 at 07:16
  • 1
    You can also use `File.AppendText(path)` to create a StreamWriter to append to a file! – Athanasios Kataras Nov 19 '20 at 07:17