共用方式為


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 information about enabling tracing, see Enabling Network Tracing.

The computer configuration file, machine.config, is stored in the %Windir%\Microsoft.NET\Framework folder in the directory where Windows was installed. 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\machine.config).

These settings can also be made in the configuration file for the application, which has precedence over the computer configuration file.

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.Sockets">
            <listeners>
              <add name="System.Net"/>
            </listeners>
          </source>
          <source name="System.Net.Cache">
            <listeners>
              <add name="System.Net"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="System.Net" value="Verbose"/>
          <add name="System.Net.Sockets" value="Verbose"/>
          <add name="System.Net.Cache" value="Verbose"/>
        </switches>
        <sharedListeners>
          <add name="System.Net"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="network.log"
          />
        </sharedListeners>
        <trace autoflush="true"/>
      </system.diagnostics>
    </configuration>
    

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

Some public methods of the Socket, TcpListener, TcpClient, and Dns classes

System.Net

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

Some public methods of the HttpListener, HttpListenerRequest, and HttpListenerResponse classes.

System.Net.Cache

Some private and internal methods in System.Net.Cache.

The attributes listed in the following table configure trace output.

Attribute name Attribute value

Value

Required String attribute. Sets the verbosity of the output. Legitimate values are Critical, Error, Verbose, Warning, and Information.

This attribute must be set on the <add name> element of the <switches> element as shown in the example. An exception is thrown if this attribute is set on the <source> element.

maxdatasize

Optional Int32 attribute. Sets the maximum number of bytes of network data included in each line trace. The default value is 1024.

This attribute must be set on the <source> element as shown in the example. An exception is thrown if this attribute is set on an element under the <switches> element.

Tracemode

Optional String attribute. Set to includehex to show protocol traces in hexadecimal and text format. Set to protocolonly to show only text. The default value is includehex.

This attribute must be set on the <switches> element as shown in the example. An exception is thrown if this attribute is set on an element under the <source> element.

See Also

Concepts

Interpreting Network Tracing
Network Tracing
Enabling Network Tracing
Introduction to Instrumentation and Tracing