如何:配置网络跟踪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.

计算机配置文件 machine.config 存储在 %windir%\Microsoft.NET\Framework 文件夹中。The computer configuration file, machine.config, is stored in the %windir%\Microsoft.NET\Framework folder. 安装在计算机上的每个版本的 .NET Framework,在 %windir%\Microsoft.NET\Framework 下的文件夹中分别有一个单独的 machine.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\Microsoft.NET\Framework\v2.0.50727\Config\machine.config C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config
  • C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config 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:

nameName 输出来自Output from
System.Net.Sockets SocketTcpListenerTcpClientDns 类的一些公共方法。Some public methods of the Socket, TcpListener, TcpClient, and Dns classes.
System.Net HttpWebRequestHttpWebResponseFtpWebRequestFtpWebResponse 类的一些公共方法,以及 SSL 调试信息(证书无效、缺少颁发机构列表以及客户端证书错误)。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.

必须在 switches 元素的 add 元素上设置此属性 。This attribute must be set on the add element of the switches element. 如果在 source 元素上设置此属性,则会引发异常 。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.

必须在 source 元素上设置此属性 。This attribute must be set on the source element. 如果在 switches 元素下的一个元素上设置此属性,则会引发异常 。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.

必须在 source 元素上设置此属性 。This attribute must be set on the source element. 如果在 switches 元素下的一个元素上设置此属性,则会引发异常 。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