如果没有听众,System.Diagnostics.Trace 什么都不做吗?

Does System.Diagnostics.Trace do nothing is there are no listeners?

我希望能够在实时程序集中保留 Trace,但前提是跟踪是 NoOp。这与我不想向事件日志发送垃圾邮件的原因相同。

那么,如果我将详细的操作信息输出到 Trace 方法,如果我附加了跟踪侦听器,它们实际上只是 stored\collected 吗?

即它们不会被写入硬盘或存储在某个地方?

MSDN: Trace Listeners:

When using Trace, Debug and TraceSource, you must have a mechanism for collecting and recording the messages that are sent. Trace messages are received by listeners. The purpose of a listener is to collect, store, and route tracing messages. Listeners direct the tracing output to an appropriate target, such as a log, window, or text file.

所以当你没有监听器时,输出将不会存储在某个地方。

你可以这样做(这是 recommended way to configure tracing anyway) in configuration:

<system.diagnostics>
  <trace>
    <listeners>
      <clear/>
    <listeners>
  </trace>
</system.diagnostics>

或来自代码:

System.Diagnostics.Trace.Listeners.Clear();