0

I was doing some trials on the basis of the following Q&A: Where does Console.WriteLine go in ASP.NET?.

The code I tried goes like below:

    var fs = new System.IO.FileStream(@"D:\log.txt", System.IO.FileMode.Append);
    var tr = new System.IO.StreamWriter(fs);
    Console.SetOut(tr);
    Console.WriteLine("My Default Debugging");
    fs.Close();

Here I am setting the FileStream fs to StreamWriter tr and in turn setting it as Console.Out by calling Console.SetOut(). So, by that I am expecting it to write to the file by Console.WriteLine(). Though my file gets created, it is empty.

What can be the thing I am missing here?

Community
  • 1
  • 1
RinoTom
  • 2,278
  • 2
  • 26
  • 40

3 Answers3

1

try tr.WriteLine("string"); instead.

Jazerix
  • 4,729
  • 10
  • 39
  • 71
  • It is a good alternative, but my intention was here to make use of `Console.WriteLine` as mentioned in the Q&A http://stackoverflow.com/questions/137660/where-does-console-writeline-go-in-asp-net – RinoTom Sep 19 '13 at 11:30
0

Becuase Console.WriteLine() doesn't do that?

Writes the specified data, followed by the current line terminator, to the standard output stream.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0
var fs = new System.IO.FileStream(@"D:\log.txt", System.IO.FileMode.Append);
var tr = new System.IO.StreamWriter(fs);
Console.SetOut(tr);
Console.WriteLine("My Default Debugging");
tr.Close();
fs.Close();

Maybe it's because you didn't close the StreamWriter before you closed the FileStream?

sekky
  • 834
  • 7
  • 14