trace元素中的autoflush属性有什么作用

What does the autoflush attribute in trace element do

我的 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 后,跟踪写入 TextWriterOutput.log。

我还注意到我可以使用

将跟踪写入 TextWriteOutput.log

System.Diagnostics.Trace.Flush();

而不是将 autoflush 属性修改为 true。

我在 https://msdn.microsoft.com/en-us/library/system.diagnostics.trace.flush(v=vs.110).aspx 但这对我来说没有意义。为什么跟踪不能立即写入输出文件?谁能用简单的话解释为什么?

Flush 方法强制将输出写入文件。将 autoflush 属性设置为 true 会导致 Trace 始终立即写入文件,而不是进行缓冲。