How to Enable WCF Trace for SharePoint

July 24, 2014

Troubleshooting when things go wrong with your WCF application in a SharePoint environment can be a nasty task. SharePoint tends to hide the actual error giving you a service fault without actually telling you what’s happening. When we can’t get the error out of our NUnit tests that we run we have to enable WCF Tracing to figure out what’s going on. Here are the steps to perform this:

1) Find and open the web.config for the site that you are trying to debug. Most cases it’s located at C:\inetpub\wwroot\wss\VirtualDirectories\WebsiteName\web.config
2) Locate the line <system.serviceModel>
3) Add the following lines inside the node:

    <diagnostics>
          <messageLogging maxMessagesToLog="30000" logEntireMessage="true" logMessagesAtServiceLevel="true" logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
    </diagnostics>

4) Next, add the following after the </system.ServiceModel>

    <system.diagnostics>
        <sources>
          <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" propagateActivity="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <source name="System.Runtime.Serialization">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="mytracelog.log" />
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>

5) Save the file and perform an IISReset
6) Now test your service, using NUnit, Fiddler, SoapUI or whatever you’re using.
7) A file named ‘mytracelog.log’ will be created in the directory your web.config was located in.
8) Use a tool called SvcTraceViewer.exe to open and read the file. The exe comes with .Net sdk, which can be found at C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin. If it’s not there, download the Mircosoft SDK. Here is some more info on the SvcTraceViewer.exe.
9) Any errors that happen will show up in the viewer as highlighted lines.

If you’re using NUnit to test your service. You can add these lines to your NUnint web.config to enable trace for your test.

<system.serviceModel>
    <diagnostics>
      <messageLogging maxMessagesToLog="30000"
              logEntireMessage="true"
              logMessagesAtServiceLevel="true"
              logMalformedMessages="true"
              logMessagesAtTransportLevel="true">
      </messageLogging>
    </diagnostics>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Verbose, ActivityTracing"
              propagateActivity="true" >
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Verbose">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="yourtracefile.log" />
    </sharedListeners>
    <trace autoflush="true" />
</system.diagnostics>

This helps take the guess work, and trial and error that you might do without enabling trace. It sure saved us a couple times.