Theory behind WCF Tracing is not an exception of classical definition of Tracing. Through Tracing an application provides information about itself. Information may vary from internal state of object to passed input parameter to method. This information should be logged, persisted or saved somewhere.
WCF Tracing can be used for the instrumentation of the WCF Service.
There are essentially four steps involved in WCF Tracing
Emitting Trace information from Service
To emit trace information WCF provides different sources. However you have choice to create trace source using TraceSource class as well. You can choose any of WCF Assembly level trace source to emit the tracing information.
WCF Assembly level Trace Sources are as below,
- System.ServiceModel
- System.ServiceModel.MessageLogging
- System.ServiceModel.IdentityModel
- System.ServiceModel.Activation
- System.Runtime.Serilization
- System.IO.Log
Setting the Trace level
You can define the level of messages you want to trace. If you are tracing all level of messages then size of message generated for tracing would be very bulky and it may affect application negatively.
WCF supports below tracing levels
Trace level information is set by using the switchValue attribute for trace source.
Configuring the listener
You need to configure a listener to persist the messages at any desired location. There must be at least one listener configured for WCF tracing.
Listener can be configured either
- Through code
- Or in configuration file.
You can configure listener directly in source element of the configuration.
Note: In further post, I will discuss in detail on configuring the listener.
Enabling Message logging
Last step you need to perform is to enable message logging in configuration. WCF logs message at two levels.
Different attributes of Message loggings are as below,
Enough of theory Now let us go ahead and capture basic trace in WCF service.
- Create a basic WCF Service
- Consume service in a client. I am assuming that you are consuming service in a console client.
- Open App.config of console client
Emit Trace Information
Add a source in System.Diagostics
Set the Trace Level
We are setting here trace level to Information and Activity tracing
Configure the Listener
We are using listener System.Diagnostics.XmlWriterTraceListener. Trace information would be find at Traces.svclog file under bin/debug folder .
Trace information gets buffered, so to make it visible you need to flush it using below configuration
After performing above three steps your System.Diagnostics should look like below,
Enable Message Logging
Last step you need to perform is enabling Message logging. Add below diagnostic in System.ServiceModel.
Eventually client side App.Config should look more or less like below,
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <diagnostics> <messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" maxMessagesToLog="3000" maxSizeOfMessageToLog="2000"/> </diagnostics> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://localhost:2934/Service1.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> <system.diagnostics> <sources> <source name="System.ServiceModel.MessageLogging" switchValue="Information, ActivityTracing"> <listeners> <add name="log" type="System.Diagnostics.XmlWriterTraceListener" initializeData="Traces.svclog"/> </listeners> </source> </sources> <trace autoflush ="true"/> </system.diagnostics> </configuration>
After configuring as said above when you run client application you will get a file in
Bin\debug\Traces.svclog
Open above file to view the trace I hope this post was useful. In further post I will take you to dipper in tracing. Thanks for reading
Leave a Reply