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?

2
  • is the same code working fine in a console application ? and i'll also close write object Commented Sep 19, 2013 at 10:35
  • I tried it in a web application. it works if i add tr.close before fs.close as sekky told. Commented Sep 19, 2013 at 10:55

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is a good alternative, but my intention was here to make use of Console.WriteLine as mentioned in the Q&A stackoverflow.com/questions/137660/…
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?

Comments

0

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

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

1 Comment

Console.WriteLine() does that and FileStream is a standard output stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.