Logging Messages in WCF Services

Windows Communication Foundation (WCF) provides features to help you monitor messages that flow through your WCF solutions and the activities that they perform. You can use the tools that WCF provides to examine the message flows and activity traces to help you diagnose faults with your services.

This e-learning product provides you with the knowledge and skills to monitor and diagnose faults with your WCF solutions.

WCF provides activity tracing and message logging, which enable you to easily monitor your WCF solutions and diagnose faults.

It is important to understand the difference between these two functions. Message logging captures the messages that are sent between your WCF service and client applications. Activity tracing provides a record of events in your solution, which enables you to monitor its behavior and diagnose any exceptions

WCF provides functionality to enable you to record the communications between your services and client applications for purposes such as debugging and troubleshooting. You can configure the WCF message logger to fit your needs. The message log that is generated gives you diagnostic information that you can use to debug problems in your WCF solution.

Techniques for Monitoring a WCF Application

WCF provides powerful tools to help you monitor and diagnose problems with your WCF solution.

From an operational perspective, you can perform message logging and activity tracing, but there are other diagnostic tools that you can use to gather Windows Management Instrumentation, or WMI, performance metrics.

Using message logging, you can capture and examine the messages that are sent between a WCF service and client applications.

Activity tracing enables you to determine how a WCF service processes these messages.

Message logging and tracing provide an alternative to using the Visual Studio 2008 debugger to examine the flow between a client application and a WCF service. Message logging enables you to monitor the behavior of your WCF solution and help diagnose any exceptions.

You can configure your WCF service to write the message log and activity trace information to a log file for inspection.

You can use the Service Trace Viewer tool to examine logged messages, and analyze the message flow between a client application and a WCF service.

However, you are not limited to writing your output to a log file.

You can use the .NET Framework trace-listener classes, which are located in the System.Diagnostics namespace. The EventLogTraceListener class provides a simple listener that directs tracing or debugging output to an event log.

The TextWriterTraceListener class directs output to a TextWriter or to a Stream such as FileStream.

The XmlWriterTraceListener class directs output as XML-encoded data to a TextWriter or a Stream

 

Message Logging in WCF

By using the message-logging functionality in WCF, you can record incoming and outgoing messages at both the client application and the service.

WCF uses the .NET Framework trace source and trace-listener mechanism to expose the flow of messages between the client application and the service. WCF provides a message-logging trace source that emits the message contents. You can then connect this to a trace listener that saves the messages for analysis by using the Service Trace Viewer tool.

Logging at the transport level records messages just before they are sent out or just after they are received from the transport layer. If your service uses a binding that implements message-level encryption, logging at this level records the encrypted messages. Logging at the service level records messages after the WCF channel stack has processed them. Inbound messages are recorded just before presentation to the service whereas outbound messages are recorded just before the channel stack processes them for transmission by the service layer. If your service uses a binding that implements message-level encryption, logging at this level records the unencrypted messages. This distinction can be important to determine where problems may be occurring.

The way that you configure your message logger will determine whether each message contains just header information or header and body information.

Typically, a message will contain the following:

  • The time when the message was sent.

  • The sender of the message.

  • The address to which the message was sent.

  • The operation to which the message relates and any parameters or return values that are defined on that operation.

When you log messages, it is important to consider the nature of the information, for example, whether it is sensitive personal data or application data. Some data may be covered by legislation such as the US Sarbanes-Oxley Act or the UK Data Protection Act. Regardless of the information, you should keep log files in a secure environment where unauthorized remote and local users cannot access them.

For more information, see Message Logging

Tags: