Logging .Net Socket Traffic

I was unsure what title to give this blog; it’s really about two things. Firstly it about tracing (or logging) network activity and secondly it’s a bit of a rant about logging that information to a file or the like.

I recently created an application built on WCF. One of the things I wanted to do was log the messages sent and received. After a quick search I realized that WCF used the standard .Net System.Diagnostics.Trace so you could capture trace from WCF by adding a few lines to your App.Config file. More recently I have been developing an app that uses a 3rd party library that uses the .Net equivalent of Sockets (rather than WCF). What I didn’t realize was that you could still trace the network traffic. Adding these few lines to my App.Config file did the trick.

<system.diagnostics>
  <trace autoflush="true" indentsize="4" />
  <sources>
    <source name="System.Net">
      <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>
  <sharedListeners>
    <add
      name="System.Net"
      type="System.Diagnostics.TextWriterTraceListener"
      initializeData="C:\Logs\WCF.log" />
  </sharedListeners>
  <switches>
    <add name="System.Net" value="Verbose" />
    <add name="System.Net.Sockets" value="Verbose" />
    <add name="System.Net.Cache" value="Verbose" />
  </switches>
</system.diagnostics>

[Just change "C:\Logs\WCF.log" to whatever you like.]

You can replace the ‘TextWriterTraceListener’ with ‘XmlWriterTraceListener’ and you should be able to use the Service Trace Viewer Tool (SvcTraceViewer.exe) to view the output; something I haven’t tried.

This brings me onto my second point; why are so many people wasting valuable time creating ‘log’ libraries. You only have to do a simple search of CodePlex to find more libraries than just about anything else; NLog, Clog, Live Labs, etc. My point is that .Net already has a good, simple interface in System.Diagnostics.Trace so why do people insist on creating something else. Why not simply provide additional TraceListeners that enhance the existing .Net Trace?

Technorati Tags: ,,
Advertisements

Tags: , ,

One Response to “Logging .Net Socket Traffic”

  1. www Says:

    This design is steller! You definitely know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job.
    I really loved what you had to say, and more than that,
    how you presented it. Too cool!

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


%d bloggers like this: