WCF Tracing and Message Logging
The most important thing when you are developing an application is actually the debugging process. If we are talking about WCF, tracing should help developer to debug WCF service by logging all operations on the service.
How to enable trace
Open the config file of the WCF service host using the Configuration editor tool (SvcConfigEditor.exe). In the Configuration editor, navigate to the Diagnostics node, and click the Enable Tracing link. This enables Tracing your WCF service and it also creates a listener (ServiceModelTraceListener) and a source (System.ServiceModel) under the Listeners and Sources folder respectively. Things that needs to be configured are pretty well self-describing so I’m not going to describe additionally.
The crucial part of this configuration part is to configure the Listener where that traces will be written. The default location for trace messages is app_tracelog.svclog file in the folder where WCF service is hosted. If you like, you can change this default value under ServiceModelTraceListener, Listeners node and change the value of the InitData attribute.
After that, our web.config file should contain the following section:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="app_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
View Trace information
Double click to the svclog file or navigate to the SvcTraceViewer.exe installation location (C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin) and run SvcTraceViewer.exe. Click on the File menu and then the Open menu. Navigate to the location where your trace files are stored.
You can notice that all internal service information is stored at low level and can be parsed additionally for debugging purposes. You can find a lot of interesting information related to processing details, processing duration and communication details. However, I found this tool very useful in the following scenarios:
Using this tool you can notice visual color helpers: items in green are successful execution, items in yellow are warnings, items in red are exceptions where you can view details of the exception in the XML dump. Make sure you scroll and look for the details of the exception. This can saves you a life in some situations…
To make a picture complete, sometimes in the debugging process you will need details about the messages itself. For that purpose, we need to configure message logging which should make our debugging process very effective.
Message Logging
Using the WCF Configuration Editor we need to enable Message logging, same as we did with the Tracing. Diagnostic Source and Listener are added automatically to our config file (default file for storing messages is app_messages.svclog), and also we’ve got few additional settings in ServiceModel Diagnostics section which helps us to identify few details on catching the messages:
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog ="1000"
maxSizeOfMessageToLog="524288"/>
Quick explanation on the most important attributes related to log throttling, especially if you include message logging in the production environment:
-
maxMessagesToLog (default: 10000)- Allows you to limit the size of the log file, by reducing the total number of messages in the log..
-
maxSizeOfMessagesToLog (default: 262144) - Allows you to limit the size of the log file in bytes, by restricting very large messages from being logged.
When the message limit is reached, a trace at the Information level is produced, and all message logging activities stop. More details on this config section and how to use them in different scenarios, can be found in this MSDN article.
Back to SvcTraceTool, you can notice at the previous screen, there is a tab named Messages in the SvcTraceTool. Using the project tab, while the trace file is loaded, right click on the Project node and Add File (app_messages.svclog) to the TraceView Project File. After adding the messages log, those merged information can be very very useful for the developers!
Conclusion
Definitely be aware and use this tool! It is a great way to inspect visually messages when things are going as expected, watch the service model trace to inspect components leading up to an exception and find the details leading up to a service fault.
Complete example of WCF service host and client can be downloaded here.