How can I configure message logging in a C# WCF application?
Posted: (EET/GMT+2)
Windows Communication Foundation (WCF for short) provides built-in message logging so you can inspect for example your SOAP requests and responses that flow through your service. WCFs logging options are most commonly configured in app.config or web.config. Yes, it is possible to configure this in code, but then the depth and nature of logging is decided by the developer, not the environment's administrator.
If you are working with SOAP (not everything is JSON, you see), you might use a configuration similar to this one:
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtTransportLevel="true"
logMessagesAtServiceLevel="true" />
</diagnostics>
</system.serviceModel>
</configuration>
After enabling this, you can open the .svclog file in the Microsoft Service Trace Viewer tool (included with the Windows SDK) to see full message contents, timestamps, and exceptions.
Message logging is invaluable when debugging SOAP faults, interoperability issues, or unexpected serialization problems in WCF applications.