设置 ServicePointManager.ServerCertificateValidationCallback 并关闭跟踪时出现 TypeInitializationException

TypeInitializationException when setting ServicePointManager.ServerCertificateValidationCallback with tracing off

当我尝试设置

时遇到未处理的 TypeInitializationException 异常
ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;

当我的 app.config 中有以下内容(取自 MSDN)时,会发生异常。与下面的配置和 MSDN 上配置的唯一区别是我将 System.NetSystem.Net.CacheSystem.Net.SocketsSystem.Net.WebSockets 的日志记录设置为 Off。我只有在所有四个都关闭时才看到问题。

这个配置有问题还是我遗漏了什么?

<system.diagnostics>
  <sources>
    <source name="System.Net" tracemode="includehex" maxdatasize="1024">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Cache">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Http">
      <listeners>
        <add name="System.Net "/>
      </listeners>
    </source>
    <source name="System.Net.Sockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.WebSockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="System.Net" value="Off"/>
    <add name="System.Net.Cache" value="Off"/>
    <add name="System.Net.Http" value="Verbose"/>
    <add name="System.Net.Sockets" value="Off"/>
    <add name="System.Net.WebSockets" value="Off"/>
  </switches>
  <sharedListeners>
    <add name="System.Net"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="network.log"
    />
  </sharedListeners>
  <trace autoflush="true"/>
</system.diagnostics>

这是异常的堆栈轨迹

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()

存在内部异常,类型也是 TypeInitializationException,调用堆栈仅包含 at System.Net.ServicePointManager..cctor(),消息为 "The type initializer for 'System.Net.ComNetOS' threw an exception."。

这有一个类型为 ConfigurationErrorsException 的最终内部异常。消息是 "Listener 'System.Net ' does not exist in the sharedListeners section.",堆栈跟踪是

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()

System.Net.Http 的侦听器名称中有一个 space。

<source name="System.Net.Http">
  <listeners>
    <add name="System.Net "/>
  </listeners>
</source>

这将尝试找到一个名为 "System.Net " 的侦听器,但当然你只有一个名为 "System.Net" 的侦听器。只需删除 space 就可以了。