.NET的Network Tracing 机制

程序之间通信跟踪:

------------------------被测程序 Sample Start ----------------------------------------------------

 using System.Diagnostics;

using System.Net;
using System.Net.Sockets;

 

private void Sample()
        {
            UdpClient client = new UdpClient(10000);
            try
            {
                client.Connect("www.google.com", 10000);
                byte[] sendBytes = Encoding.ASCII.GetBytes("Hello?");
                client.Send(sendBytes, sendBytes.Length);

                client.Close();
            }
            catch (Exception e)
            {
                // Todo : Exeception handle
             }
        }

--------------------end-----------------------------------------------

 

 

 

 

 

Network Tracing 提供了访问托管程序产生的网络通信信息,以及网络方法的调用信息机制。要利用这种机制,需要添加如下的配置文件:

---------------------App.config-----------------------------------------

 

 <?xml version="1.0" encoding="utf-8" ?>
<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>

  

 

--------------------------end ------------------------------------------------

 

在主程序中调用Sample(),将会在当前目录下生成network.log的文件,

 

----------------------------network.log------------------------------------------

System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Socket(InterNetwork#2)
System.Net Information: 0 : [5268] Current OS installation type is 'Client'.
System.Net.Sockets Verbose: 0 : [5268] Exiting Socket#7746814::Socket()
System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Bind(0:10000#10000)
System.Net.Sockets Verbose: 0 : [5268] Exiting Socket#7746814::Bind()
System.Net.Sockets Verbose: 0 : [5268] DNS::GetHostAddresses(www.google.com)
System.Net.Sockets Verbose: 0 : [5268] DNS::GetHostByName(www.google.com)
System.Net.Sockets Verbose: 0 : [5268] Exiting DNS::GetHostByName()  -> IPHostEntry#63840421
System.Net.Sockets Verbose: 0 : [5268] Exiting DNS::GetHostAddresses()  -> IPAddress[]#54246671
System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Connect(147:10000#-1813941567)
System.Net.Sockets Information: 0 : [5268] Socket#7746814 - Created connection from 207.46.92.16:37891 to 209.85.225.147:10000.
System.Net.Sockets Verbose: 0 : [5268] Exiting Socket#7746814::Connect()
System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Send()
System.Net.Sockets Verbose: 0 : [5268] Data from Socket#7746814::Send
System.Net.Sockets Verbose: 0 : [5268] 00000000 : 48 65 6C 6C 6F 3F                               : Hello?
System.Net.Sockets Verbose: 0 : [5268] Exiting Socket#7746814::Send()  -> 6#6
System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Close()
System.Net.Sockets Verbose: 0 : [5268] Socket#7746814::Dispose()
System.Net.Sockets Verbose: 0 : [5268] Exiting Socket#7746814::Close()

---------------------------end--------------------------------------------

通过log,可以清楚的看到网络程序调用的方法,发送呵接受的数据,这些都是问题诊断所需要的重要信息。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值