29

enter image description here

I thought this would be simple, but I can't see how to tell WCF to log message bodies. I have:

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel" switchValue="Verbose">
      <listeners>
        <add type="System.Diagnostics.DefaultTraceListener" name="Default">
          <filter type="" />
        </add>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EntLibLoggingProxyTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging"
          name="traceListener">
          <filter type="" />
        </add>
      </listeners>
    </source>
  </sources>
</system.diagnostics>
<system.serviceModel>
   <diagnostics>
     <messageLogging logEntireMessage="true" logMalformedMessages="true"
       logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" />
   </diagnostics>
   ...etc..,
   ...etc...
</system.Model>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
   <listeners>
     <add fileName="_trace-xml.log"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,Microsoft.Practices.EnterpriseLibrary.Logging"
                traceOutputOptions="None"
                type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging"
        name="XML Trace Listener" />      
     ...etc... other listeners
  </listeners>
  ...etc...
</loggingConfiguration>

But all I get logged is stuff about the message, not the message body. What do I need to change to log message content?

5 Answers 5

41

Just following this description works perfectly for me: http://msdn.microsoft.com/en-us/library/ms730064.aspx

<system.diagnostics>
<sources>
  <source name="System.ServiceModel.MessageLogging">
    <listeners>
             <add name="messages"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="c:\logs\messages.svclog" />
      </listeners>
  </source>
</sources>
</system.diagnostics>

<system.serviceModel>
<diagnostics>
<messageLogging 
     logEntireMessage="true" 
     logMalformedMessages="false"
     logMessagesAtServiceLevel="true" 
     logMessagesAtTransportLevel="false"
     maxMessagesToLog="3000"
     maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>

No need to write any code for that ...

Sign up to request clarification or add additional context in comments.

13 Comments

If I do this I get tons of info about what WCF is doing, but nothing about the contents of the messages:
grrr. Anyway the above attempts to demonstrate that the svclog element for 'process action 'getBlah' the 'sent a message over a channel' entry does not in fact log the message in question, but just has an empty <MessageHeaders></MessageHeaders> and no MessageBody or anysuch. Other trace items such as 'a message was written' and 'Http response was received also contain no message info. Not sure how to post a screenshot.
Screenshot posted, although you'd need several to prove that none of the tracerecords contain a message body? Having spent some time yesterday googling on this I got the impression that JohnC was right and that something like msdn.microsoft.com/en-us/library/… was the solution that blogs tended to imitate. I'm on Net3.5 if it makes a difference
Okay: I confirm that the Messages Tab (in the top left Activity, Project, Message, Graph tabgroup) IS empty. (As is the Message tab in the bottom right Formatted-Xml-Message tabgroup). We could hypothesis that logEntireMessage just works in VS2010/.Net 4; and just doesn't work in Vs2008/.Net 3; and this is why there is a spattering of blogs and a MS magazine article on the route indicated by JohnC's response. Or there is something crucial I've missed; but I have the same config as you've indicated in your answer and it don't work for me.
This works. You may want to increase maxSizeOfMessageToLog
|
3

I found an versioning issue. When the WCF Service project was built against .Net 3.5 I saw the full message body in the viewer tool. Built against .Net 4.0 I saw no message body.

2 Comments

Ahhh so irritating! This seems to be it!.. one of those "you only need it when something goes wrong".
To the people having trouble with this on .Net 4.0. Pay attention, Tracing is: <source name="System.ServiceModel"> Messages are: <source name="System.ServiceModel.MessageLogging"> stackoverflow.com/questions/11264260/…
1

You can consider another way to see XML SOAP - custom MessageEncoder. The main difference from similar IDispatchMessageInspector / IClientMessageInspector is that it works on lower level, so it captures original byte content including any malformed xml.

In order to implement tracing using this approach you need to wrap a standard textMessageEncoding with custom message encoder as new binding element and apply that custom binding to endpoint in your config.

Also you can see as example how I did it in my project - wrapping textMessageEncoding, logging encoder, custom binding element and config.

Comments

0

Try http://www.csharptutorial.in/12/csharp-net-wcf-how-to-enable-wcf-tracing-and-messagelogging and setup logMessagesAtTransportLevel="true". In your log file you can find <s:Body> tags. Investigate text near them. Later you can add new "XmlListener" and found the message body in more flexible format.

Comments

-1

If you need message bodies, you'll have to capture them yourself, programatically. Use a service behavior to install a message inspector and capture messages inside your implementation of IDispatchMessageInspector or IClientMessageInspector.

5 Comments

Alas. So does something like this blog wcfpro.wordpress.com/2011/03/29/iclientmessageinspector give me everything I need or is there more that I need to know in terms of config or code to log message bodies?
That link is pretty much everything you need. The only thing it doesn't show is how to install the endpoint behavior, which is pretty easy (endpoint.Behaviors.Add).
This answer is wrong. Configuring a source for System.ServiceModel.MessageLogging and a listener to log the messages works fine for me. There is no message inspector required.
Wrong! As @TomW said, you can simply configure a source to receive the message.
For others inclined to call this answer wrong and claim it all just works in config, I refer you to @RichardHowells' answer : “I found an versioning issue. When the WCF Service project was built against .Net 3.5 I saw the full message body in the viewer tool. Built against .Net 4.0 I saw no message body.”

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.