WCF is all about handling the messages from different systems. From a developer’s perspective, Message Logging may need to find out the exact message, transmitted over a period of time. For example, you may want to know the messages that are logged at the transport layer (For debugging purposes).
Of course, it’s highly recommended not to enable the ‘Message Logging’ options in the production environment. In this article, we are going to discuss what various available options there are to log the messages and how we control the Message Logging.
What comes to your mind when you think about message logging? Obviously, you wonder where do we need the log and what messages are logged there. Yes, “where” comes before “what”. So, here is the answer. The listener will take care of “Where we need to log the data”, and Message Logging will take care of “What we need to log”. These two elements are essential to log your messages, <System.Diagnostics> enables you to interact with event logs and Performance counters,
Following are the details you need to mention in <System.Diagnostics>
- Switch value or Trace Level
- Initialize Data (Path of the log file)
Switch Value – Tracing settings can be done in the Switch value, Following are the Trace Levels,
- Off – Tracing Disabled, ignores the tracing
- Critical – Critical issues like Application crashed or shut immediately, Tracing will be generated for critical issues in service
- Error – Logs all the exceptions
- Warning – Tracing will be generated for warnings which indicate the possibility of problems
- Information – Tracing will be generated for the successful execution of application
- Verbose – Mostly used for debugging purpose. it allows low-level tracing
- Activity Tracing – Tracing will be generated for the communication across components of an application.
In the above snapshot, you can find the source as Message Logging. That means diagnostics has to interact with the System.ServiceModel.MessageLogging to record the event data. IntializeData gets file path where we need to log the data. XmlWriteTraceListener will create the traces in the XML Format.
Once we are done with the diagnostics part, we need to take the Message Logging part to complete the Configuration as I have mentioned in the below snapshot.
- LogEntireMessage – Do you need to log the entire message including header and body? Default value is False.
- maxMessagesToLog – What is the maximum number of messages to be logged? Ex- 1000 messages, Messages will not be logged if it goes beyond this level 1000. This will save you from performance overhead of your service.
- maxSizeOfMessageToLog – What is the maximum size of a message to log? Ex- 2000 bytes. It helps you to decide the size of a message the log.
- logMessagesAtServiceLevel – Do you want to log the messages that are about to enter the service(On receiving)? Do you want to log the messages that are about to leave the service (On Sending).
- logMessagesAtTransportLevel – Do you want the messages which are ready to be encoded (On Sending) and transportation over the wire? Do you want the messages which are ready to be decoded (On Receiving) after transportation over the wire?
- logMalFormedMessages – What’s the meaning of Malformed Messages? It’s all about the WCF messages that are rejected by the WCF runtime during processing. Use this element to log the messages that are rejected by the WCF Stack
Please find the below snapshot of app.config file, and refer the attached source code for the complete reference.
Please use this link to download the source-code
To log and analyse the happenings in the server side, Message Logging will be a perfect option in WCF. There are two things we need to define in the message logging, Listener ( to write the data in a particular location) and Message logging (to log the messages at the service level, transport level).