我的web.config文件中有以下代码:
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
在我的代码背后的某处:
System.Diagnostics.Trace.WriteLine("From the trace");
运行应用程序后,TextWriterOutput.log文件已成功创建,但为空白。但是,在将autoflush属性更改为true后,trace会写入TextWriterOutput.log。
我还注意到我可以使用
将跟踪写入TextWriteOutput.logSystem.Diagnostics.Trace.Flush();
而不是将autoflush属性修改为true。
我读过它 https://msdn.microsoft.com/en-us/library/system.diagnostics.trace.flush(v=vs.110).aspx 但这对我没有意义。为什么跟踪无法立即写入输出文件?谁能用简单的话解释为什么?
答案 0 :(得分:3)
Flush
方法强制将输出写入文件。将autoflush
属性设置为true
会导致Trace始终立即写入文件,而不是缓冲。