How to get detailed connection logging info in .NET

Sometimes when you connect to an external host and you get for example the error “The request was aborted: Could not create SSL/TLS secure channel”, you want to have more detailed info about what causes the error.

.NET has a build in diagnostics meganism that will give you more detailed info. The following have to be added to de web.config:

   1:  <configuration>
   2:      
   3:      <system.diagnostics>
   4:          <trace autoflush="true" />
   5:          <sources>
   6:              <source name="System.Net" maxdatasize="1024">
   7:                  <listeners>
   8:                      <add name="MyTraceFile"/>
   9:                  </listeners>
  10:              </source>
  11:              <source name="System.Net.Sockets" maxdatasize="1024">
  12:                  <listeners>
  13:                      <add name="MyTraceFile"/>
  14:                  </listeners>
  15:              </source>
  16:              <source name="System.Net.HttpListener">
  17:                  <listeners>
  18:                      <add name="MyTraceFile"/>
  19:                  </listeners>
  20:              </source>
  21:              <source name="System.Net.Cache">
  22:                  <listeners>
  23:                      <add name="MyTraceFile"/>
  24:                  </listeners>
  25:              </source>
  26:          </sources>
  27:   
  28:          <sharedListeners>
  29:              <add
  30:                name="MyTraceFile"
  31:                type="System.Diagnostics.TextWriterTraceListener"
  32:                initializeData="System.Net.trace.log"
  33:                  />
  34:          </sharedListeners>
  35:          <switches>
  36:              <add name="System.Net" value="Verbose" />
  37:              <add name="System.Net.Sockets" value="Verbose" />
  38:          </switches>
  39:      </system.diagnostics>
  40:   
  41:  </configuration>

The detailed log info will be written to a file “System.Net.trace.log” in the root of your web application. In the log you will find info like for example this:

System.Net Information: 0 : [1796] SecureChannel#53793566 - Certificate is of type X509Certificate2 and contains the private key.
System.Net Information: 0 : [1796] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent  = Outbound, scc     = System.Net.SecureCredential)
System.Net Error: 0 : [1796] AcquireCredentialsHandle() failed with error 0X8009030D.
System.Net Information: 0 : [1796] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent  = Outbound, scc     = System.Net.SecureCredential)
System.Net Error: 0 : [1796] AcquireCredentialsHandle() failed with error 0X8009030D.
System.Net.Sockets Verbose: 0 : [1796] Socket#17610470::Dispose()
System.Net Error: 0 : [1796] Exception in the HttpWebRequest#30822392:: - The request was aborted: Could not create SSL/TLS secure channel.
System.Net Error: 0 : [1796] Exception in the HttpWebRequest#30822392::EndGetResponse - The request was aborted: Could not create SSL/TLS secure channel.
System.Net.Sockets Verbose: 0 : [4632] Socket#24383397::Dispose()

More info about system.diagnostics you will find here.

No Comments