Want to see the http requests your asp.net code is making?

A lot of projects I work on involve integrating with a third party API. It could be as simple as a few calls to enhance a section, or complete reliance on an API to provide the backbone for an application.

However deep your integration, it is vital to understand what happening as calls are made from your application to an API. Without the detail of the requests, the contents of each request, and the repsonse provided by the API, your development time will be considerably increased. It’s the same as trying to debug an application by just ‘console.logging’ variable values. Without better tools, you’re going to be there a long time.

Tools like Fiddler or Charles will show calls between the front and back end of your applciation, but once it leaves the back end of your asp.net application, the request disappears in to a a black hole.

After struggling with this for many months I found a Stack Overflow post (of course) which suggested adding network tracing via the web.config to log requests and responses. So, by adding the following magic to the web.config

<system.diagnostics>
    <trace autoflush="true" /> 
    <sources>
        <source name="System.Net" maxdatasize="1024">
            <listeners>
                <add name="VisualStudioConsole"/>
            </listeners>
        </source>
    </sources>
    <sharedListeners>
            <add name="VisualStudioConsole" type="System.Diagnostics.ConsoleTraceListener" />
    </sharedListeners>
    <switches>
        <add name="System.Net" value="Verbose" />
    </switches>
</system.diagnostics>

The following data is logged to the Output window in Visual Studio….

System.Net Verbose: 0 : [9712] 00000000 : 7B 22 4D 65 6D 62 65 72-4E 75 6D 62 65 72 22 3A : {"UserNumber":
System.Net Verbose: 0 : [9712] 00000010 : 34 34 34 37 37 2C 22 43-61 6C 63 75 6C 61 74 69 : 44477,"Calculati
System.Net Verbose: 0 : [9712] 00000020 : 6F 6E 4E 61 6D 65 22 3A-22 4C 45 41 56 45 52 51 : onName":"LEAVEQ
System.Net Verbose: 0 : [9712] 00000030 : 55 4F 54 45 22 2C 22 50-61 72 61 6D 65 74 65 72 : UOTE","Parameter
System.Net Verbose: 0 : [9712] 00000040 : 73 22 3A 5B 7B 22 50 61-72 61 6D 65 74 65 72 4E : s":[{"ParameterN
System.Net Verbose: 0 : [9712] 00000050 : 61 6D 65 22 3A 22 43 41-4C 43 44 22 2C 22 50 61 : ame":"CALCD","Pa
System.Net Verbose: 0 : [9712] 00000060 : 72 61 6D 65 74 65 72 56-61 6C 75 65 22 3A 22 30 : rameterValue":"0
System.Net Verbose: 0 : [9712] 00000070 : 36 2F 30 31 2F 32 30 31-36 22 7D 5D 7D          : 6/01/2016"}]}

The configuration added to the web.config above creates a listener to System.Net. This logs some public methods of the HttpWebRequest and HttpWebResponse classes (amongst others). Full details are available on MSDN for configuring network tracing in your application.

The output above shows the body of a request that was sent to an api – in this case a json object. In amongst all that noise, you can just about see some curly braces. Although the output format is not ideal, it does provide the essential visibility that did not exist before.

This is a quick and dirty way to start logging the API requests your application is making. If anyone as anything better, please add a comment below.