Skip to content

How to capture SOAP messages

jimper edited this page Oct 8, 2014 · 8 revisions

Introduction

When debugging Ads API calls, one important resource is the SOAP messages you send to the server. Here are some ways to capture the SOAP logs.

Option 1: Use SOAP logs

The Ads API .NET client library logs SOAP requests through System.Trace. You can control the settings through your application’s App.config / Web.config files. An example default config file is shown below:

<system.diagnostics>
  <sources>
    <source name="AdsClientLibs.SoapXmlLogs"
      switchName="AdsClientLibs.SoapXmlLogs"
      switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <!-- Modify the initializeData attribute below to control the
              path to the SOAP XML log file. -->
        <add name="soapXmlLogListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="C:\Logs\soap_xml.log" />
        <remove name="Default" />
      </listeners>
    </source>
    <source name="AdsClientLibs.RequestInfoLogs"
      switchName="AdsClientLibs.RequestInfoLogs"
      switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <!-- Modify the initializeData attribute below to control the
              path to the request info log file. -->
        <add name="requestInfoLogListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="C:\Logs\request_info.log" />
        <remove name="Default" />
      </listeners>
    </source>
  </sources>
  <switches>
    <!-- Use this trace switch to control the SOAP XML logs written by Ads*
        .NET libraries. The default level is set to Off. Logs are generated at
        both the Error and Information levels. -->
    <add name="AdsClientLibs.SoapXmlLogs" value="Off"/>
    <!-- Use this trace switch to control the Request Info logs written by
        Ads* .NET libraries. The default level is set to Off. Logs are
        generated at both the Error and Information levels. -->
    <add name="AdsClientLibs.RequestInfoLogs" value="Off"/>
  </switches>
</system.diagnostics>

Logging can be enabled or disabled by editing the value attribute of the AdsClientLibs.SoapXmlLogs and AdsClientLibs.RequestInfoLogs trace switches. See this MSDN article for a list of all possible switch values, but note that only a subset of these values are used by this library:

Value Impact on logging
Off Nothing will be logged.
Error Errors will be logged.
Information Errors and debugging information will be logged.

To enable the most verbose logging (Information), you would make the following changes:

<add name="AdsClientLibs.SoapXmlLogs" value="Information"/>
<add name="AdsClientLibs.RequestInfoLogs" value="Information"/>

See this wiki article for more details on App.config.

Option 2: Use Fiddler

You can use Fiddler, an HTTP(s) proxy to capture traffic from the Ads API .NET client library. Once you launch Fiddler, it will start acting as an HTTP(s) proxy server, running on localhost:8888. To capture HTTPS traffic, you need to turn on HTTPS decryption. This can be done by following the instructions on http://www.fiddler2.com/Fiddler/help/OptionsUI.asp.

Now, set the ProxyServer configuration setting in your application’s App.config / Web.config to http://localhost:8888 and run your application.

Option 3: Use System.Net tracing

Options 1 and 2 are good for most situations, but if you absolutely need the raw logs for the HTTP(S) calls you make, then you can use System.Net tracing options. You need to add the following lines to your App.config to achieve this:

<system.diagnostics>
  <trace autoflush="true" />
  <sources>
    <source name="System.Net">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Sockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Cache">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add
      name="System.Net"
      type="System.Diagnostics.TextWriterTraceListener"
      initializeData="System.Net.trace.log"/>
  </sharedListeners>
  <switches>
    <add name="System.Net" value="Verbose" />
    <add name="System.Net.Sockets" value="Verbose" />
    <add name="System.Net.Cache" value="Verbose" />
  </switches>
</system.diagnostics>

When you run your application, the detailed trace will be written to System.Net.trace.log.