Debug.Writeline

2019-08-28 21:09发布

I am trying to write some messages to a text file using Debug.WriteLine("Message").

Here is the code.

Dim Tr As TextWriterTraceListener
Tr = New TextWriterTraceListener(System.IO.File.CreateText("Output1.txt"))
'Tr = New TextWriterTraceListener(System.Console.Out)
Debug.Listeners.Add(Tr)
Debug.WriteLine("Test Message")

I see the output1.txt file being created, but nothing is being written in the file.

How can I fix this problem?

3条回答
时光不老,我们不散
2楼-- · 2019-08-28 21:10

The TextWriterTraceListener buffers data and only writes it when the buffer is full, it is closed/disposed, or Flush() is called:

Dim Tr As TextWriterTraceListener
Tr = New TextWriterTraceListener(System.IO.File.CreateText("Output1.txt"))
'Tr = New TextWriterTraceListener(System.Console.Out)
Debug.Listeners.Add(Tr)
Debug.WriteLine("Test Message")
Tr.Flush()
查看更多
啃猪蹄的小仙女
3楼-- · 2019-08-28 21:10

You need to flush the buffer of the TextWriterTraceListener.

After writing to the trace listener you need to:

Tr.Flush();

See the documentation for Flush on MSDN.

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-28 21:34

You have to close and dispose the TextWriterTraceListener

Tr.Close()
Tr.Dispose()
查看更多
登录 后发表回答