共用方式為


追蹤和訊息記錄的建議設定

本主題描述不同作業環境的建議追蹤和訊息記錄設定。

針對生產環境,如果您使用 WCF 追蹤來源,請將 設定 switchValue 為 [警告]。 如果您使用 WCF System.ServiceModel 追蹤來源,請將 switchValue 屬性設定為 Warning ,並將 propagateActivity 屬性設定為 true。 如果您使用使用者定義的追蹤來源,請將 switchValue 屬性設定為 Warning, ActivityTracing。 這可以使用組態編輯器工具手動完成(SvcConfigEditor.exe)。 如果您不預期效能會受影響,您可以在所有先前提及的案例中將switchValue屬性設定為Information,這會產生相當大量的追蹤數據。 下列範例示範這些建議的設定。

<configuration>
 <system.diagnostics>
  <sources>
    <source name="System.ServiceModel"
            switchValue="Warning"
            propagateActivity="true" >
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
    <source name="myUserTraceSource"
            switchValue="Warning, ActivityTracing">
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="xml"
         type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="C:\logs\Traces.svclog" />
  </sharedListeners>
 </system.diagnostics>

<system.serviceModel>
  <diagnostics wmiProviderEnabled="true">
  </diagnostics>
 </system.serviceModel>
</configuration>

針對部署或偵錯環境,選擇 InformationVerbose,以及 ActivityTracing 作為使用者定義或 System.ServiceModel 的追蹤來源。 若要增強偵錯,您也應該將額外的追蹤來源 (System.ServiceModel.MessageLogging) 新增至組態,以啟用訊息記錄。 請注意,屬性 switchValue 不會影響此追蹤來源。

下列範例示範了建議的設定,使用一個利用 XmlWriterTraceListener 的共用接聽程式。

<configuration>
 <system.diagnostics>
  <sources>
    <source name="System.ServiceModel"
            switchValue="Information, ActivityTracing"
            propagateActivity="true" >
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
    <source name="System.ServiceModel.MessageLogging">
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
    <source name="myUserTraceSource"
            switchValue="Information, ActivityTracing">
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="xml"
         type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="C:\logs\Traces.svclog" />
  </sharedListeners>
 </system.diagnostics>

 <system.serviceModel>
  <diagnostics wmiProviderEnabled="true">
      <messageLogging
           logEntireMessage="true"
           logMalformedMessages="true"
           logMessagesAtServiceLevel="true"
           logMessagesAtTransportLevel="true"
           maxMessagesToLog="3000"
       />
  </diagnostics>
 </system.serviceModel>
</configuration>

使用 WMI 修改設定

你可以在執行時使用 WMI 更改設定設定(如前述設定範例所示,啟用 wmiProviderEnabled 設定中的屬性)。 例如,你可以在 CIM Studio 中使用 WMI,在執行時將追蹤來源的等級從警告調整為資訊。 您應該知道,以這種方式進行即時偵錯的效能成本可能很高。 如需使用 WMI 的詳細資訊,請參閱 使用 Windows Management Instrumentation for Diagnostics 主題。

啟用 ASP.NET 追蹤中的相互關聯事件

除非開啟 ASP.NET 事件追蹤,否則 ASP.NET 事件不會設定相互關聯標識碼 (ActivityID)。 若要正確查看相互關聯的事件,您必須在命令控制台中使用下列命令開啟 ASP.NET 事件追蹤,您可以移至 [開始]、[ 執行 ] 並輸入 cmd 來叫用。

logman start mytrace -pf logman.providers -o test.etl -ets

若要關閉 ASP.NET 事件的追蹤,請使用下列命令:

logman stop mytrace -ets

另請參閱