Tracing System.Net

Often, the last secret weapon you have when troubleshooting networked applications is a packet sniffer. If you ever need one, don’t bother looking around, grab a copy of ethereal.

But there are also situations where sniffing is not easily possible, e.g. when you are using transport security like SSL, when you use the loobback address or you simply can’t install a sniffer on the machine.

In .NET 2.0 you can enable tracing for the whole .NET network stack (aka System.Net). This can be done on a per-application basis using a configuration file. The trace output is really interesting. It shows the process and thread ID, the payload of the data (regardless of transport security as tracing happens much earlier) and lots of interesting protocol information.

To see what’s going on in the network stack simply add the following to a configuration file:

<system.diagnostics>

 

  <trace autoflush=true />

  <sources>

    <source name=System.Net>

      <listeners>

        <add name=TraceFile />

      </listeners>

    </source>

  </sources>

 

  <sharedListeners>

    <add

      name=TraceFile

      type=System.Diagnostics.TextWriterTraceListener

      initializeData=NetTrace.log />

  </sharedListeners>

 

  <switches>

    <add name=System.Net value=Verbose />

  </switches>

 

</system.diagnostics>

and do a simple network operation like:

new WebClient().DownloadData(http://www.leastprivilege.com&#8221;);

and inspect the NetTrace.log file.

You can have even more fun with stuff like web service proxies or the AuthenticatedStream classes.

Very useful!

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s