Wednesday, August 22, 2007

Enable Internal System.Net Logging

Having trouble with certificates while requesting information using HttpWebRequest, manifesting itself with the error "Could not create SSL/TLS secure channel". As far as we could tell the certificate should have been ok.

To diagnose the problem I found this great blog post to enable the internal logging from the System.Net classes by Durgaprasad Gorti over at the MSDN Blogs.

Just needs a new section in the app.Config file to log out what the System.Net classes are doing. One of the samples in the blog post worked straight off for me:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <system.diagnostics>
        <trace autoflush="true" />
            <sources>
                <source name="System.Net" maxdatasize="1024">
                    <listeners>
                        <add name="MyTraceFile"/>
                    </listeners>
                </source>
              <source name="System.Net.Sockets" maxdatasize="1024">
                    <listeners>
                        <add name="MyTraceFile"/>
                    </listeners>
                </source>  
           </sources>



            <sharedListeners>
                <add
                  name="MyTraceFile"
                  type="System.Diagnostics.TextWriterTraceListener"
                  initializeData="System.Net.trace.log"
                />
            </sharedListeners>
            <switches>
                <add name="System.Net" value="Verbose" />
              <add name="System.Net.Sockets" value="Verbose" />
            </switches>
    </system.diagnostics>
</configuration>


It creates a file called System.Net.trace.log in the directory where the EXE is running. The logging is very comprehensive and will point you in the right direction for fixing your problems.

No comments: