This article describes how to use WCF tracing and message logging with various options. With WCF tracing from System.Diagnostics you can configure trace source, emit traces, activity tracing, and its propagation.
WCF tracing enables you to diagnose data, fault messages, and its analysis. Tracing is a better option than debugging to understand the application's behavior and its flow. Tracing gives you the details of all application components like fault and code exceptions, system events, operation calls. Depending on the Switch Value traces will be generated.
WCF tracing is not enabled by default you need to configure it with the switch value and tracelistener.
Switch Value: Switch value decides what level of trace needs to be generated. Below default values are available for Switch Value
Trace Listener: WCF services and clients add trace data to listeners configured app.config file. There are a couple of predefined trace listeners like XmlWriterTraceListener which can be used to save trace. The initializeData property must be set to some physical file.
Go through the article for creating a service library Create new WCF service library and test using WCFTestClient.
Host Service as suggested in Hosting WCF service in IIS. or hosting in Windows service
Open the app.config file of the WCF service library and add the configuration for system.diagnostics.
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Critical,Information,ActivityTracing"
propagateActivity="true">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\logs\messages.svclog" />
</listeners>
</source>
</sources>
<trace autoflush="true" />
</system.diagnostics>
In this configuration we are using "Critical, Information, ActivityTracing" as switch value. This will generate traces for all unhandled exceptions, successful execution and communication between two components.
XmlWriterTraceListener will create the traces in XML format. The property value of initializeData will mention the physical location of the trace file. <trace autoflush="true" /> will flush the traces to file automatically.
Execute some of the service operations using client application. The file messages.svclog must have created under C:\logs folder. Open the file by using SvcTraceViewer.exe.
Message logging allows you to log the entire request and response message to log files. For enabling message logging you need to use trace listener System.ServiceModel.MessageLogging and properties of <messagelogging>
System.ServiceModel.MessageLogging has below attributes :Add below message logging configuration to .config file.
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\logs\northwindservices.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="false"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="500"
maxSizeOfMessageToLog="5000"/>
</diagnostics>
</system.serviceModel>