如何:配置網路跟蹤How to: Configure network tracing

應用程式或電腦組態檔都會保存可決定網路追蹤格式和內容的設定。The application or computer configuration file holds the settings that determine the format and content of network traces. 在執行這個程序之前,請確認已啟用追蹤。Before performing this procedure, be sure tracing is enabled. 有關詳細資訊,請參閱啟用網路跟蹤For more information, see Enable network tracing.

電腦設定檔 ,電腦.config,存儲在 %windir%\Microsoft.NET_Framework資料夾中。The computer configuration file, machine.config, is stored in the %windir%\Microsoft.NET\Framework folder. 在 %windir%_Microsoft.NET_Framework的資料夾中,對於電腦上安裝的每個版本的 .NET Framework,有一個單獨的電腦.config檔,例如:There is a separate machine.config file in the folders under %windir%\Microsoft.NET\Framework for each version of the .NET Framework installed on the computer, for example:

  • C:_WINDOWS_微軟.NET_Framework_v2.0.50727_機器。C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config
  • C:_WINDOWS_微軟.NET_Framework64_v4.0.30319\機器。C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config

您也可以在應用程式的組態檔中進行這些設定,它的優先順序高於電腦組態檔。These settings can also be made in the configuration file for the application, which has precedence over the computer configuration file.

配置網路跟蹤Configure network tracing

要配置網路跟蹤,請將以下行添加到相應的設定檔。To configure network tracing, add the following lines to the appropriate configuration file. 下表中會說明這些設定的值和選項。The values and options for these settings are described in the tables below.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="includehex" maxdatasize="1024">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Http">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.WebSockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
   </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
      <add name="System.Net.Http" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.WebSockets" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
        traceOutputOptions="ProcessId, DateTime"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
</configuration>

從方法跟蹤輸出Trace output from methods

當您將名稱加入至 <switches> 區塊時,追蹤輸出就會包括來自與該名稱相關的某些方法的資訊。When you add a name to the <switches> block, the trace output includes information from some methods related to the name. 下表描述了輸出:The following table describes the output:

名稱Name 輸出來源Output from
System.Net.Sockets SocketTcpListenerTcpClientDns類的一些公共方法。Some public methods of the Socket, TcpListener, TcpClient, and Dns classes.
System.Net HttpWebRequestHttpWebResponseFtpWebRequest類以及FtpWebResponseSSL 調試資訊(無效證書、缺少頒發者清單和用戶端憑證錯誤)的某些公共方法。Some public methods of the HttpWebRequest, HttpWebResponse, FtpWebRequest, and FtpWebResponse classes, and SSL debug information (invalid certificates, missing issuers list, and client certificate errors).
System.Net.HttpListener HttpListenerHttpListenerRequestHttpListenerResponse 類別的一些公用方法。Some public methods of the HttpListener, HttpListenerRequest, and HttpListenerResponse classes.
System.Net.Cache System.Net.Cache 中的一些私用和內部方法。Some private and internal methods in System.Net.Cache.
System.Net.Http HttpClientDelegatingHandlerHttpClientHandlerHttpMessageHandlerMessageProcessingHandlerWebRequestHandler 類別的一些公用方法。Some public methods of the HttpClient, DelegatingHandler, HttpClientHandler, HttpMessageHandler, MessageProcessingHandler, and WebRequestHandler classes.
System.Net.WebSockets.WebSocket ClientWebSocketWebSocket 類別的一些公用方法。Some public methods of the ClientWebSocket and WebSocket classes.

跟蹤輸出屬性Trace output attributes

下表中列出的屬性配置跟蹤輸出:The attributes listed in the following table configure trace output:

屬性名稱Attribute name 屬性值Attribute value
value 必要的 String 屬性。Required String attribute. 設定輸出的詳細等級。Sets the verbosity of the output. 合法值為 CriticalErrorVerboseWarningInformationLegitimate values are Critical, Error, Verbose, Warning, and Information.

必須在交換器元素的添加元素上設置此屬性。This attribute must be set on the add element of the switches element. 如果在元素上設置了此屬性,則引發異常。An exception is thrown if this attribute is set on the source element.

範例: <add name="System.Net" value="Verbose"/>Example: <add name="System.Net" value="Verbose"/>
maxdatasize 選擇性 Int32 屬性。Optional Int32 attribute. 設定每一行追蹤所包含之網路資料的最大位元組數。Sets the maximum number of bytes of network data included in each line trace. 預設值為 1024。The default value is 1024.

必須在元素上設置此屬性。This attribute must be set on the source element. 如果在交換器元素下的元素上設置此屬性,則引發異常。An exception is thrown if this attribute is set on an element under the switches element.

範例: <source name="System.Net" tracemode="includehex" maxdatasize="1024">Example: <source name="System.Net" tracemode="includehex" maxdatasize="1024">
tracemode 選擇性 String 屬性。Optional String attribute. 設定為 includehex 以便使用十六進位和文字格式來顯示通訊協定追蹤。Set to includehex to show protocol traces in hexadecimal and text format. 設定為 protocolonly 則只會顯示文字。Set to protocolonly to show only text. 預設值是 includehexThe default value is includehex.

必須在元素上設置此屬性。This attribute must be set on the source element. 如果在交換器元素下的元素上設置此屬性,則引發異常。An exception is thrown if this attribute is set on an element under the switches element.

範例: <source name="System.Net" tracemode="includehex" maxdatasize="1024">Example: <source name="System.Net" tracemode="includehex" maxdatasize="1024">

另請參閱See also