Click here to Skip to main content
Click here to Skip to main content

WCF Tracing FAQs

, 29 Apr 2009
Rate this:
Please Sign up or sign in to vote.
WCF Tracing FAQs

Table of Contents

Introduction and Goal

In this article, we will look at how we can trace and debug information in WCF services. There are some ready made tracelisteners provided by WCF. The base of these ready made trace listeners is .NET trace listener. So we will first understand the basic concept of trace listener and then go through the ready made tracelisteners provided by WCF.

Now a days, I am distributing my 400 questions and answers ebook which covers major .NET related topics like WCF, WPF, WWF, Ajax, Core .NET, SQL Server, Architecture and a lot more. I am sure you will enjoy this ebook.

WCF Basic Questions and Answers

If you are new to WCF, I will recommend that you give one read to my WCF FAQs before reading this article. It will help you to understand WCF fundamentals so that you can understand this article much better.

Can You Explain the Concept of Trace Listener?

Tracelisteners are objects that get tracing information from the trace class and they output the data to some medium. For instance, you can see from the figure TraceListener how it listens to the trace object and outputs the same to UI, File or a Windows event log. There are three different types of tracelisteners, the first is the defaulttracelistener (this outputs the data to UI), the second is textwritertracelistener (this outputs to a file) and the final one is ‘Eventlogtracelistener’ which outputs the same to a Windows event log.

Figure: TraceListener

Below is a code snippet for textwritertracelistener and eventlogtracelistener. Using textwritertracelistener, we have forwarded the traces to ErrorLog.txt file and in the second snippet, we have used the Eventlogtracelistener to forward the traces to Windows event log.

Figure: Tracelistener in action

What are the Readymade Trace Events and They Are Available for Which WCF Objects ?

You can always use the core Tracelistener events provided by .NET, but WCF has readymade trace listeners for the core WCF objects.

Assembly Name Description
System.ServiceModel Logs the following:-
  • Message process
  • Reading of configuration information
  • Transport-level action
  • Security requests
System.ServiceModel.MessageLogging Generates tracing information for every message that flows through the system.
System.ServiceModel.IdentityModel Generates trace data for authentication and authorization.
System.ServiceModel.Activation Emits information regarding activation of the service.
System.Runtime.Serialization Emits information when objects are serialized or deserialized. WCF always serializes and de-serializes information during request so it’s a good event to see the content of the request.
System.IO.Log Emits messages with respect to Common Log File System (CLFS).
CardSpace Emits trace messages related to any CardSpace identity processing that occurs within WCF context.

How Can We Enable Tracing on the Readymade Tracing WCF Objects?

We will enable tracing on System.ServiceModel tracing object. To enable tracing, we need to enable the system.diagnostics XML node in the web.config file of the WCF service. We need to also define the type of listeners for the System.ServiceModel listener object. So we add the listeners tag with the type as System.Diagnostics.XmlWriterTraceListener. We need to also define the file and path where the file is created. For the current scenario, we have defined the file as Traces.svclog and the folder as c:\ drive.

<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing">
<listeners>
<add name="log"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

Now if you run the WCF service, you can see an XML file created as shown below:

#<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>0</EventID>
<Type>3</Type>
<SubType Name="Transfer">0</SubType>
<Level>255</Level>
<TimeCreated SystemTime="2009-04-30T03:21:09.5625000Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" 
	RelatedActivityID="{d11829b7-d2db-46d5-a4ac-49a37a56376e}" />
<Execution ProcessName="WebDev.WebServer" ProcessID="2660" ThreadID="8" />
<Channel/>
<Computer>COMPAQ-JZP37MD0</Computer>
</System>
<ApplicationData></ApplicationData>
</E2ETraceEvent>

What is the Concept of tracelevel in Trace Listeners?

In the previous question, we have specified switch value as information. This value indicates what type and level of tracing information you want to record. Below is the list of the same.

Trace Level Description
Off Ignore all trace messages
Critical Log unexpected processing events or unhandled exceptions have occurred. The application will terminate immediately or shortly.
Error An unexpected processing event or exception has occurred. The application is still capable of continuing its processing.
Warning Indicates there is a possible problem but the application will continue running.
Information Application is running smoothly only that informative message is recorded. In this case, messages are just milestones.
Verbose Very similar to information but provides more details as compared.
ActivityTracing In this case, messages are related to communication between components and the activities.
All In this case, all messages are captured.

Trace level value is specified in source tag in switch value. For instance the below web.config snippet indicates the trace type as Information.

<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing">
............
............
............
............

What is a Service Level Message and Transport Level Message?

You can log WCF messages at two levels, one is service level and the other is transport level. Service level: In this, the messages are logged as they enter the user code or leave the user code.
Transport level: In this, the messages are logged as they are ready to be encoded / decoded. All transport level, infrastructure messages and also reliable messaging is logged. You specify the message levels in the diagnostics node as shown in the below code snippet.

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

Messagelogging also has other attributes. Below is the short description about the same.

Attribute Description
logEntireMessage Should the entire message be logged on only the header.
logMalformedMessages Should malformed messages be logged.
logMessagesAtServiceLevel Should service-level messages be logged.
logMessagesAtTransportLevel Should transport level messages be logged.
maxMessageToLog Number indicating how many messages should be logged.
maxSizeOfMessageToLog The default value is 256Kb. Max size of the message log.

History

  • 30th April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
GeneralMy vote of 1 Pinmemberj43j10-Jul-12 4:41 
GeneralMy vote of 4 Pinmemberbasheer97118-Jul-11 1:30 
GeneralProgrammatic overrides PinmemberMember 126058017-Feb-11 14:47 
If I have configured my trace listeners in my web.config/ap.config file. How can can I override these programmtically.
For example if I have
 
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Off,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
 
and I need to change the switchValue to say warning
 
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Warning,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
 
one can simply edit the appropriate config file. However for end users one does not want them to edit these files.
What one would like is to have these values established from the config files and then have an option within one's application
to alter these settings programmtically so only specific values (switchValue in this case) are changed. One would normally not want tracing on but if a ser has problems the user can set activate the trace to some level and then e-mail back the trace log for analysis.
 
So one would like
1. Start application
2. Apllication reads in config file settings.
3. [Optionally read in user preferences from a separate source (file, database etc) and use these to override config file settings]
4. Provide the option for the user to programmtically override some settings while application is running.
5. [Optionally persist user override preferences to another storage mechanism (file, database etc)]
 

One does not want to edit or persist changes to the original config files. If one needs persist changes then one would have a separate persistant store with overrides and apply any changes after the original config file is processed. However for the most part one would only like a temporary override while the application is running.
 
Is this possible?
 
From what research I have done one can set up these settings programmatically or from a config file. There does not seem to be any information on how to have a hybrid of the two, use the config file to establish base values and then programmatically override some.
GeneralGood article Pinmembermicro-mike4-May-09 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 30 Apr 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid