启用网络跟踪Enabling Network Tracing

网络跟踪允许访问有关方法调用的信息,以及有关托管应用程序所生成的网络流量的信息。Network tracing provides access to information about method invocations and network traffic generated by a managed application. 必须完成以下任务才能在应用程序中启用网络跟踪:You must complete the following tasks to enable network tracing in your application:

最常见的跟踪目标(也称为跟踪侦听器)是默认侦听器和日志文件。The most common trace destinations, also referred to as trace listeners, are the default listener and the log file.

如果不指定跟踪侦听器,则跟踪使用默认侦听器。Tracing uses the default listener if you do not specify a trace listener. 可通过在启用托管代码的调试器中执行代码,查看发送到默认侦听器的消息,例如 .NET Framework SDK 附带的 CLR 调试器 或 Windows SDK 附带的 DBwin32.exe。You can view messages sent to the default listener by executing your code in a managed code-enabled debugger such as the CLR Debugger shipped with the .NET Framework SDK, or DBwin32.exe shipped with the Windows SDK. 使用 CLR 调试器时,跟踪消息将出现在“输出”窗口中 。Using the CLR Debugger, the trace messages appear in the Output window.

如果希望使用文件接收跟踪,则可使用配置设置指定日志文件,如以下示例所示。If you prefer to use a file to receive traces, you can specify a log file by using configuration settings, as shown in the following example. (有关配置文件的一般讨论,请参阅配置文件。)(For a general discussion of configuration files, see Configuration Files.)

若要将跟踪发送到日志文件,请将以下节点添加到相应配置文件(应用程序或计算机)的 <system.diagnostics> 节点。To send traces to a log file, add the following node to the <system.diagnostics> node of the appropriate configuration file (application or machine). 可按需更改文件的名称 (trace.log)。You can change the name of the file (trace.log) to suit your needs.

<system.diagnostics>  
  <trace autoflush="true" indentsize="4">  
    <listeners>  
      <add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log"/>  
    </listeners>   
  </trace>  
</system.diagnostics>  

请参阅See also