views:

284

answers:

2
+2  Q: 

WCF REST Debugging

How can I log what xml is sent to my WCF REST service prior to being deserialized into my datacontract class?

A: 

If you want to look at the raw HTTP traffic, a proxy tool like Fiddler is the simplest way. You'll be able to see all the information that's been POST/PUT'd to your REST service.

If you mean "log" as in "always write the HTTP traffic to a specific location on file," then you can use the built-in tracing to do most of that. Here is a link to an example of doing this, otherwise just look for "WCF tracing" online. You'll find a ton of great examples.

joshua.ewer
+1  A: 

You can use WCF tracing to log the raw XML messages.

Tracing is not enabled by default. You can enable and configure tracing by editing the application’s configuration file. The following .config example enables WCF tracing with raw message logging:

<configuration>
  <system.serviceModel>
    <diagnostics>
      <messageLogging maxMessagesToLog="30000"
              logEntireMessage="true"
              logMessagesAtServiceLevel="true"
              logMalformedMessages="true"
              logMessagesAtTransportLevel="true">
      </messageLogging>
    </diagnostics>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.IdentityModel" 
              switchValue="Verbose" 
              logKnownPii="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- Log all messages in the 'Messages' tab of SvcTraceViewer. -->
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- ActivityTracing and propogateActivity are used to 
           flesh out the 'Activities' tab in SvcTraceViewer to 
           aid debugging. -->
      <source name="System.ServiceModel" 
              switchValue="Error, ActivityTracing" 
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- This records Microsoft.IdentityModel generated traces, 
           including exceptions thrown from the framework. -->
      <source name="Microsoft.IdentityModel" switchValue="Warning">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" 
           type="System.Diagnostics.XmlWriterTraceListener" 
           initializeData="C:\logs\trace.svclog" />
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
</configuration>

You can read more about WCF Tracing from MSDN: Configuring Tracing.

Microsoft provides a Service Trace Viewer Tool to read .svclog files.

Make sure the path defined in initializeData is writable by your service.

Daniel Vassallo
Service Trace Viewer is awesome.
Jay