This project is read-only.

Log Extension

Overview – Whenever we consume any soap web service in our apps, we also in need of logging the soap request and response for debugging the issues that may occur in future. Soap Extension is the only way to log soap response and request in .Net.

Although, we can write code using soap extension for logging but many times we don’t have time or want to write code for such small things and want to use some ready to use library which can easily be consumed and configured to provide soap request and response logging facility. This extension provides this facility to us. This is an easy to use and completely configurable extension which would surely save some dev time for us.

Features - Following are some of the features of this extension.

1. Easy to use.
2. Completely configurable.
3. We can specify our own method for logging by implementing the interface exposed by the library. Extension will call the method and will provide all the information that we want to log.
4. If we do not want to implement the interface then we can also write a trace listener or can use existing trace listeners for logging.
5. If we are consuming more than one service then we can enable logging for only those services for which we want to have logging enabled.
6. We can also enable/disable logging for web methods.


Sample logs created by LogExtension when custom logger type is used with string output

We can always manipulate this by using class ouput.

Soap Request
<?xml version="1.0"?>
<SoapMessageDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:
xsd="http://www.w3.org/2001/XMLSchema">
  <CallCount>1</CallCount>
  <RequestType>Request</RequestType>
  <ServiceUrl>http://localhost/SampleWebService/TestService.asmx</ServiceUrl>
  <SoapEnvelop>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:
xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/200
1/XMLSchema">
      <soap:Body>
        <HelloWorld xmlns="http://tempuri.org/" />
      </soap:Body>
    </soap:Envelope>
  </SoapEnvelop>
  <SoapVersion>Soap11</SoapVersion>
</SoapMessageDetails> 

Soap Response
<?xml version="1.0"?>
<SoapMessageDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:
xsd="http://www.w3.org/2001/XMLSchema">
  <CallCount>1</CallCount>
  <RequestType>Response</RequestType>
  <ServiceUrl>http://localhost/SampleWebService/TestService.asmx</ServiceUrl>
  <SoapEnvelop>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:
xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/200
1/XMLSchema">
      <soap:Body>
        <HelloWorldResponse xmlns="http://tempuri.org/">
          <HelloWorldResult>Hello World</HelloWorldResult>
        </HelloWorldResponse>
      </soap:Body>
    </soap:Envelope>
  </SoapEnvelop>
  <SoapVersion>Soap11</SoapVersion>
</SoapMessageDetails>

Configuration -

<wsExtensionsConfiguration>
  <!--loggertype could be either TraceSource or Custom-->
  <soapMessageLogger name="TraceTest" loggerType="Custom">
    <traceSource name="TraceTest"/> 
    <!--output could be either string or class-->
    <custom type="ConsoleTester.MySoapLogger, ConsoleTester" output="String"/>
    <soapMessageLogSettings logSoapRequest="true" logSoapResponse="true" logSoapHeaders="true" logServiceUrl="true" logSoapVersion="true" logCallType="true"  logCallCount="true" enableCallCounting="true">
      <types>
        <add name="ConsoleTester.TestService.TestService" hosts="localhost" logSoapRequest="true" logSoapResponse="true" logSoapHeaders="false" logServiceUrl="true" logSoapVersion="true" logCallType="false" logCallCount="true" enableCallCounting="true">
          <methods>				    	           
            <add name="HelloWorld" logSoapRequest="true" logSoapResponse="true" logSoapHeaders="false" logServiceUrl="true" logSoapVersion="true" logCallType="false" logCallCount="true" enableCallCounting="true"/>
          </methods>
        </add> 
      </types>
    </soapMessageLogSettings>
  </soapMessageLogger>
</wsExtensionsConfiguration>

<wsExtensionsConfiguration>
wsExtensionsConfiguration (Required)– This works as a container for configuration settings of all the extensions provided by this library.


<soapMessageLogger name="TraceTest" loggerType="Custom">
soapMessageLogger (Required) – Container for Log Extension config settings.
Name (Required) – This represents the logger name.
LoggerType (Required) – Type of the logger you want to use. It can either be custom or TraceSource.
Custom - If it is set to custom then you would have to implement the ISoapLogger interface to provide the method for logging.
TraceSource - If it is set to TraceSource then you would have to configure a trace source for logging.

<traceSource name="TraceTest"/>
Name (Required) – Name of the trace source to be used for logging. This trace source should be defined in system.diagnostics section.

Here is an example of how to configure trace source. I have used predefined console trace listener.
<system.diagnostics>
    <sources>
      <source name="TraceTest" switchName="SourceSwitch"
        switchType="System.Diagnostics.SourceSwitch" >
        <listeners>
          <add name="console" />
          <remove name ="Default" />
        </listeners>
      </source>
    </sources>
    <switches>
      <!-- You can set the level at which tracing is to occur -->
      <add name="SourceSwitch" value="Information" />
      <!-- You can turn tracing off -->
      <!--add name="SourceSwitch" value="Off" -->
    </switches>
    <sharedListeners>
      <add name="console"
        type="System.Diagnostics.ConsoleTraceListener"
        initializeData="false"/>
    </sharedListeners>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="console" />
      </listeners>
    </trace>
  </system.diagnostics>

<custom type="ConsoleTester.MySoapLogger, ConsoleTester" output="String"/>
Custom - This is used to specify the type which you created for logging by implementing ISoapLogger.
Type (Required) – Type which has been created for logging after implementing ISoapLogger.
Output (Required) – Output could either be string or class. Interface has two methods which you can implement for logging as per your requirement. One method provides information as a string while other provides information as an object of SoapMessageDetails class. In case if output is selected as string then the method which provide information as string is called by extension or if output is selected as class then the method which provided information as class is called.

Example of how to implement ISoapLogger :
public class MySoapLogger : ISoapLogger
{
	#region Public Methods
 
        /// <summary>
        /// Gets the SOAP message details.
        /// </summary>
        /// <param name="messageLogType">Type of the message log.</param>
        /// <param name="message">The message.</param>
		public void GetSoapMessageDetailsForLogging(SoapMessageLogType messageLogType, SoapMessageDetails message)
		{
			Serializer<SoapMessageDetails> serializer = new Serializer<SoapMessageDetails>(SerializerType.XmlSerializer);
			string str = serializer.Serialize(message);
			Console.Write(str);
			Console.Read();
		}
 
        /// <summary>
        /// Gets the SOAP message details.
        /// </summary>
        /// <param name="messageLogType">Type of the message log.</param>
        /// <param name="message">The message.</param>
	    public void GetSoapMessageDetailsForLogging(SoapMessageLogType messageLogType, string message)
	    {
            Console.Write(message);
	    }
 
	#endregion Public Methods
}

<soapMessageLogSettings logSoapRequest="true" logSoapResponse="true" logSoapHeaders="true" logServiceUrl="true" logSoapVersion="true" logCallType="true"  logCallCount="true" enableCallCounting="true">
soapMessageLogSettings (Optional) - Settings specified at this level applies on all the services consumed in the application.
logSoapResponse (Optional) (Default - True) - Used to specify whether soap response need to be logged or not.
logSoapRequest (Optional) (Default - True) - Usd to specify whether soap request need to be logged or not.
logSoapHeaders (Optional) (Default - True) - Used to specify whether soap headers need to be logged or not.
logServiceUrl (Optional) (Default - True) - Used to specify whether service endpoint url need to be logged or not.
logSoapVersion (Optional) (Default - True) - Used to specify whether sap version need to be logged.
logCallType (Optional) (Default - True) - Used to specify whether call type(sync or async) need to be logged.
logCallCount (Optional) (Default - True) - Used to specify whether call counts need to be logged or not.
enableCallCounting (Optional) (Default - True) - Used to enable/disable call counting.

<types>
<add name="ConsoleTester.TestService.TestService" hosts="localhost" logSoapRequest="true" logSoapResponse="true" logSoapHeaders="false" logServiceUrl="true" logSoapVersion="true" logCallType="false" logCallCount="true" enableCallCounting="true">
<methods>
<add name="HelloWorld" logSoapRequest="true" logSoapResponse="true" logSoapHeaders="false" logServiceUrl="true" logSoapVersion="true" logCallType="false" logCallCount="true" enableCallCounting="true"/>
</methods>
</add> 
</types>

types (Optional) - If multiple services are consumed in the application then this element can be used to enable/disable logging for specific services.
name (Optional) - Represents the type name. You can find this in reference.cs file created by .Net framework when you refer a service. We create an object of this type when you want to call a web method of the service.
hosts (Optional) - Using this setting you can enable logging for a particulat host only. i.e if you want to log only if request is made to the service hosted locally then you can use "localhost".
logSoapRequest logSoapResponse logSoapHeaders logServiceUrl logSoapVersion logCallType logCallCount enableCallCounting - Same as above`but at this level these are used to override the value defined at parent (soapMessageLogSettings ) level.

methods (Optional) - Used if you want to enable/disable logging at web method levels.
logSoapRequest logSoapResponse logSoapHeaders logServiceUrl logSoapVersion logCallType logCallCount enableCallCounting - Same as above`but at this level these are used to override the value defined at parent (types) level.



CallCounter Extension

Overview - This extension can be used to count the calls being made to the web services. You can use this extension if you need to generate reports.

Features - Following are some of the features of this extension.

1. Easy to use.
2. Completely configurable.

Configuration -

 <wsExtensionsConfiguration>
    <callCounterSettings enableCallCounting="true">
      <types>
        <add name="ConsoleTester.TestService.TestService"  enableCallCounting ="true">
          <methods>
            <add name="HelloWorld" enableCallCounting="true"/>
          </methods>
        </add>
      </types>
    </callCounterSettings>
 <wsExtensionsConfiguration>


Other configurations required to setup these extensions
<configSections>
    <section name="wsExtensionsConfiguration" type="WebServiceExtensions.Configurations.Common.WebServiceExtensionsSettings, WebServiceExtensions" />
<configSections>
<wsExtensionsConfiguration>
    <callCounterSettings enableCallCounting="true">
      <types>
        <add name="ConsoleTester.TestService.TestService"  enableCallCounting ="true">
          <methods>
            <add name="HelloWorld" enableCallCounting="true"/>
          </methods>
        </add>
      </types>
    </callCounterSettings>
    <!--loggertype could be either TraceSource or Custom-->
    <soapMessageLogger name="TraceTest" loggerType="Custom">
      <traceSource name="TraceTest"/> 
      <!--output could be either string or class-->
      <custom type="ConsoleTester.MySoapLogger, ConsoleTester" output="String"/>
      <soapMessageLogSettings logSoapRequest="true" logSoapResponse="true" logSoapHeaders="true" logServiceUrl="true" logSoapVersion="true" logCallType="true" logCallCount="true" enableCallCounting="true">
        <types>
          <add name="ConsoleTester.TestService.TestService" hosts="localhost" logSoapRequest="true" logSoapResponse="true" logSoapHeaders="false" logServiceUrl="true" logSoapVersion="true" logCallType="false" logCallCount="true" enableCallCounting="true"/>
        </types>
      </soapMessageLogSettings>
    </soapMessageLogger>
  </wsExtensionsConfiguration>


Apart from above config settings we also need to register the extension with web service life cycle. Following are the two ways to do this.

1. We can call the methods exposed by the library.

LogExtensionManager.RegisterSoapLogExtension();
CallCounterManager.RegisterCallCounterExtension();

2. Add following in config file.

<system.web>
<webServices>
<soapExtensionTypes>
<add type="WebServiceExtensions.LogExtension.SoapMessageLogExtension,
WebServiceExtensions"/>
<add type="WebServiceExtensions.CallCounterExtension.CallCounterExtension,
WebServiceExtensions" />
<soapExtensionTypes>
<webServices>
<system.web>

Last edited Oct 27, 2011 at 7:44 PM by atulsrivas1, version 19

Comments

No comments yet.