title: TypeInitializationException when setting ServicePointManager.ServerCertificateValidationCallback with tracing off
body: <p>I'm getting an unhandled <code>TypeInitializationException</code> exception when I try to set</p>
  
  <pre><code>ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) =&gt; true;
  </code></pre>
  
  <p>The exception happens when I've got the following (taken from <a href="https://msdn.microsoft.com/en-us/library/ty48b824%28v=vs.110%29.aspx" rel="nofollow">MSDN</a>) in my app.config. The only difference from the config below and that on MSDN is that I've set logging for <code>System.Net</code>, <code>System.Net.Cache</code>, <code>System.Net.Sockets</code> and <code>System.Net.WebSockets</code> to <code>Off</code>. I only see the issue when all four are off.</p>
  
  <p>Is there something wrong with this configuration or am I missing something else?</p>
  
  <pre><code>&lt;system.diagnostics&gt;
    &lt;sources&gt;
      &lt;source name="System.Net" tracemode="includehex" maxdatasize="1024"&gt;
        &lt;listeners&gt;
          &lt;add name="System.Net"/&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
      &lt;source name="System.Net.Cache"&gt;
        &lt;listeners&gt;
          &lt;add name="System.Net"/&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
      &lt;source name="System.Net.Http"&gt;
        &lt;listeners&gt;
          &lt;add name="System.Net "/&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
      &lt;source name="System.Net.Sockets"&gt;
        &lt;listeners&gt;
          &lt;add name="System.Net"/&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
      &lt;source name="System.Net.WebSockets"&gt;
        &lt;listeners&gt;
          &lt;add name="System.Net"/&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
    &lt;/sources&gt;
    &lt;switches&gt;
      &lt;add name="System.Net" value="Off"/&gt;
      &lt;add name="System.Net.Cache" value="Off"/&gt;
      &lt;add name="System.Net.Http" value="Verbose"/&gt;
      &lt;add name="System.Net.Sockets" value="Off"/&gt;
      &lt;add name="System.Net.WebSockets" value="Off"/&gt;
    &lt;/switches&gt;
    &lt;sharedListeners&gt;
      &lt;add name="System.Net"
           type="System.Diagnostics.TextWriterTraceListener"
           initializeData="network.log"
      /&gt;
    &lt;/sharedListeners&gt;
    &lt;trace autoflush="true"/&gt;
  &lt;/system.diagnostics&gt;
  </code></pre>
  
  <p>Here's the exception's stack track </p>
  
  <pre><code>at System.Net.ServicePointManager.get_ServerCertificateValidationCallback()
  at test.Program.Main(String[] args) in c:\Users\Eoin\Documents\Visual Studio 2013\Projects\test\test\Program.cs:line 12
  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>There's an inner exception, also of type TypeInitializationException, and the call stack contains only <code>at System.Net.ServicePointManager..cctor()</code> and the message is "The type initializer for 'System.Net.ComNetOS' threw an exception.".</p>
  
  <p>This has a final inner exception of type <code>ConfigurationErrorsException</code>. The message is "Listener 'System.Net ' does not exist in the sharedListeners section." and the stack trace is</p>
  
  <pre><code>at System.Diagnostics.ListenerElement.GetRuntimeObject()
  at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
  at System.Diagnostics.TraceSource.Initialize()
  at System.Net.Logging.InitializeLogging()
  at System.Net.Logging.get_On()
  at System.Net.ComNetOS..cctor()
  </code></pre>