Archive for the ‘WCF’ Category

Logging .Net Socket Traffic

March 30, 2010

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.

  <trace autoflush="true" indentsize="4" />
    <source name="System.Net">
        <add name="System.Net"/>
    <source name="System.Net.Sockets">
        <add name="System.Net"/>
    <source name="System.Net.Cache">
        <add name="System.Net"/>
      initializeData="C:\Logs\WCF.log" />
    <add name="System.Net" value="Verbose" />
    <add name="System.Net.Sockets" value="Verbose" />
    <add name="System.Net.Cache" value="Verbose" />

[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: ,,