本文主要是介绍.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,可以清楚的看到网络程序调用的方法,发送呵接受的数据,这些都是问题诊断所需要的重要信息。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/grtmn/archive/2010/04/25/5526482.aspx
这篇关于.NET的Network Tracing 机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!