Tracing and Configuring Asp.Net WCF Services to log messages

Tracing & Configuring WCF Services to log messages. And how to view log files using Service Trace Viewer.

 Asp.Net   
 

Posted: May 23, 2017. | By: mustafa

Tracing in Asp.Net WCF services enables us to log tracing information and exceptions in a log file on server. System.Diagnostic namespace provides tracing elements, it is same as tracing in Asp.Net Web application.

Steps to follow -
1. Add WCF Trace Sources
2. Add Trace level
3. Configuring Trace Listener
4. Enabling message logging
5. Use Service Trace Viewer to open log file

Add WCF Trace Sources


WCF provides different sources to emit trace information.
We have the following options:

 

  • System.ServiceModel
  • System.ServiceModel.MessageLogging
  • System.ServiceModel.IdentityModel
  • System.ServiceModel.Activation
  • System.Runtime.Serialization
  • System.IO.Log
  • Cardspace


In the configuration file add source name as:

<source name="System.ServiceModel.MessageLogging">


Add Trace level


Next we are going to add Trace Level, default value is Off.

  1. Off – This will disable message loging.
  2. Critical – add messages for critical event such as application failure.
  3. Error – logs all error and exceptions.
  4. Warning - A condition that may subsequently result in an error or critical failure.
  5. Information -
  6. Verbose – Debug level messages for both user code and http services such a incorrect endpoint.
  7. ActivityTracing - Flow events between processing activities and components. And traces for transfer.
  8. All – will write all information.


<source name="System.ServiceModel.MessageLogging"

                               switchValue="Information">


Configuring a trace listener
Add listener of type System.Diagnostics.XmlWriterTraceListener. A tracing information will be saved to messages.svclog file in web root directory.

 

<listeners>
    <add name="messages"

            type="System.Diagnostics.XmlWriterTraceListener"

            initializeData="messages.svclog" />
</listeners>


Enabling message logging
Add messageLogging  tag inside diagnostics node in System.ServiceModel to enable message loging.

 

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

 

Message loging attributes

  • logEntireMessage: By default only the message header is logged but if we set it to true, entire message including message header and body will be logged.
  • logMalformedMessages: log messages that are rejected by WCF stack at any stage are known as malformed messages.
  • logMessagesAtServiceLevel: messages those are about to enter or leave user code.
  • logMessagesAtTransportLevel: messages those are about to encode or decode.
  • maxMessagesToLog: maximum quota for messages. Default value is 10000.
  • maxSizeOfMessageToLog: message size in bytes.


Here we have the complete configuration settings.

    <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="ServiceModelTraceListener">
          </add>
        </listeners>
      </source>
    <source name="System.ServiceModel" switchValue="Information, ActivityTracing">
        <listeners>
          <add name="ServiceModelTraceListener">
          </add>
        </listeners>
      </source> 
    </sources>
    <sharedListeners>
      <add initializeData="WebMessages.svclog" type="System.Diagnostics.XmlWriterTraceListener"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>
    <system.serviceModel>
      <diagnostics>
        <messageLogging logEntireMessage="true"
                       logMessagesAtServiceLevel="false"
                       logMessagesAtTransportLevel="false"
                       logMalformedMessages="true"
                       maxMessagesToLog="3000"
                       maxSizeOfMessageToLog="2000">
        </messageLogging>
      </diagnostics>
    </system.serviceModel>


Note: we have set trace autoflush property to true so that messages would not be buffered and will be writen to log as soon as it occurs.

In the above configuration we have added two sources first one is System.ServiceModel.MessageLogging and another is System.ServiceModel namspace for activity tracing. We have also added shared listener with the name ServiceModelTraceListener to be consumed in both the trace sources declared above.


Use Service Trace Viewer to open log file

Microsoft Service Trace Viewer it helps to easily merge, view, and filter trace messages, it helps to diagnose and verify WCF service issues.

Go to start and launch the WCF Service Trace Viewer (svctraceviewer.exe), now select to open svclog log file from the path.

It contains XML data, data that was sent to and from our service, exception details, the data contract we received and returned from our service. 


ServiceTraceViewer.png, Asp.Net, C#, Asp.Net Tracing, WCF,


related posts

Back to top