Using Tracing and Message Logging Capabilities

Implementing tracing and implementing message logging capabilities are similar tasks in WCF. Therefore, we will cover them together. We'll first clarify what circumstances dictate message logging and then cover where you use tracing.

Message Logging

You can use message logging to monitor the activity of incoming and outgoing messages from a service. This will enable you to view and evaluate the message content that is received by the services. This is valuable in tracking malformed messages for system administrators and developers. The business users will also be interested in the content that describes the user's input requests that are derived through the message log. Several options are available for message logging in WCF.

Message logging can occur at two levels: the service level and the transport level. At the service level, messages get logged immediately, and at the transport level messages are logged when the WCF runtime calls the transport mechanism to transfer the messages. WCF messaging offers three switches to manage this activity. They are logMessagesAtServiceLevel, logMalformedMessages, and logMessagesAtTransportLevel, which are set in the messageLogging element. You can also utilize filters to set the boundaries for capturing messages.

At the service level, all messages are logged even if filters are defined. If they are defined, only the messages that agree with the filters are logged. This happens before the WCF runtime is called. The transport layer messages are ready to be encoded after reaching the WCF runtime. If the filters are defined, it will log only those messages that correspond to the filter. Otherwise, it will record all messages. You'll now learn how to activate message logging and define filters.

You need to be careful with assigning read access to message logging and tracing. Traditionally, only system administrators should have the privileges to activate or deactivate these processes. This could be a security breach in your solution architecture.


Enabling Message Logging

The following are the steps for using SvcConfigEditor.exe to enable message logging.

NOTE

We are using the TradeServiceHost project's App.config file to demonstrate message logging and tracing. It is important to note that production services should not be targeted to rigorous tracing and message logging. This will affect the productivity of the service, which primarily should facilitate the business processes, not tracing and message logging.

  1. Open SvcConfigEditor.exe.

  2. Open the TradeServiceHost project's App.config file (File Open).

  3. Navigate to the Diagnostics window, and click the Enable Message Logging link. This action will add a ServiceModelMessageLoggingListener class to the project to enable message logging. You can also configure the extent of the logging (where the log is stored on disk). Your screen should look like Figure 6-6.

    Enabling message logging
  4. Save the file in the Service Configuration Editor. Build the solution, and run the server and the client. You should view the message log file in the specified location. It is interesting to revisit the App.config file to view the changes by enabling message logging, as shown in Listing 6-8.

Example. Additional Code in the TradeServiceHost App.config File
...
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging"
							             switchValue="Warning, ActivityTracing">
							        <listeners>
							          <add type="System.Diagnostics.DefaultTraceListener"
                       name="Default">
							            <filter type="" />
							          </add>
							          <add name="ServiceModelMessageLoggingListener">
							             <filter type="" />
							          </add>
							        </listeners>
							      </source>
							    </sources>

    <sharedListeners>
							      <add initializeData="
							              C:PracticalWcfChapter06WCFManagement\WcfSimpleClient
							           App_messages.svclog"
							        type="System.Diagnostics.XmlWriterTraceListener, System,
							            Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
							        name="ServiceModelMessageLoggingListener"
							              traceOutputOptions="Timestamp">
							        <filter type="" />
							      </add>
							    </sharedListeners>
							  </system.diagnostics>
							<system.serviceModel>
							    <diagnostics>
							      <messageLogging logMalformedMessages="true"
							                 logMessagesAtServiceLevel="false"
							        logMessagesAtTransportLevel="true" />
							    </diagnostics>
...

How do you add a filter to the message logger? You can just include XML tags that direct the WCF runtime to log the images that correspond to this namespace and ignore the others. Listing 6-9 details the modification to the App.config file. You need to add the <filters> section to the <messageLogging> section of the App.config file. How do you view these message log files? We will show how to use the SvcTraceViewer.exe utility for this (discussed in the next section).

Example. Adding a Filter to the Message Log
<messageLogging logEntireMessage="true"
     logMalformedMessages="true" logMessagesAtServiceLevel="true"
     logMessagesAtTransportLevel="true" maxMessagesToLog="420">
     <filters>
        <add xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                        /soap:Envelope/soap:Headers
        </add>
     </filters>
</messageLogging>

Enabling Tracing

We'll now cover how to enable tracing. How does tracing differentiate from message logging? Tracing mechanisms act as the instrumentation for service messages and fault monitoring. It is similar to a Visual Studio debugger that helps you step through and step into code. Therefore, tracing is primarily a great tool to track the message flow of the application.

How do you enable tracing in WCF? Please follow steps 1 to 4 in the previous section. You need to click the Enable Tracing hyperlink to enable tracing, as in Figure 6-6. This will add a ServiceModelTraceListener instance to the runtime. You configure the name and location for your trace file if you have specific requirements.

Then build the solution, and run the server and client. All the communication for initializing the host, the communication between the host and the client, and the destruction of the host instance will be recorded in this trace file. How will you be able to view this content? You can use SvcTraceViewer.exe.

Using SvcTraceViewer.exe

The SvcTraceViewer.exe utility will enable you to view both message log files and trace files. You can find it at <Drive Name>:Program FilesMicrosoft SDKsWindowsv6.0Bin. Open the trace file from the TradeServiceHost communications with the WCFSimpleClient console application. (Navigate to the correct directory, and select File Open to open the file.) You should see a screen similar to Figure 6-7.

SvcTraceViewer.exe reading the trace file

This is a comprehensive implementation of the step-by-step process of the WCF service. It starts with the object creation at the top and records each interaction with the WCF runtime and the clients. It clearly details object activities, message sends, and all errors in the host's life cycle. You can view each of the XML messages in the bottom pane. It also records the duration of each activity. You can also get a graphical timeline representation by clicking the Graph tab. This is a comprehensive tool that adds a lot of value for developers and system administrators who are investigating tracing and message log files. Please refer to the SvcTraceViewer help file (in the same directory) for further information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset