用于跟踪 HTTP 请求的 CustomTraceListener class 实现

CustomTraceListener class implementation for tracing HTTP requests

我一直在尝试使用 CustomTraceListener class 来设置我自己的自定义跟踪侦听器。配置是按照 MSDN 完成的,但失败并显示错误消息:

 System.Configuration.ConfigurationErrorsException was unhandled by user code
  HResult=-2146232062
  Message=Could not create TraceLib.DebugTraceListener,TraceLib.
  Source=mscorlib
  BareMessage=Could not create TraceLib.DebugTraceListener,TraceLib.
  Line=0
  StackTrace:
Server stack trace: 
   at System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type baseType, String initializeData)
   at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
   at System.Diagnostics.ListenerElement.GetRuntimeObject()
   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.WebRequest.Create(Uri requestUri, Boolean useUriBase)

配置:

<system.diagnostics>
<sources>
<source name="System.Net" >
<listeners>
<add name="MyTrace"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="MyTrace" type="TraceLib.DebugTraceListener,TraceLib" initializeData="System.Net.trace.log"/>
</sharedListeners>
<switches>
<add name="System.Net" value="Information" />
</switches>
</system.diagnostics>

有线索吗?

修复了配置,它成功了!

<system.diagnostics>
<sources>
<source name="System.Net" >
<listeners>
<add name="MyTrace"/>
</listeners>
</source>
</sources>    
<sharedListeners>
<add name="MyTrace" type="TraceLib.DebugTraceListener,TraceLib" />
</sharedListeners>
<switches>
<add name="System.Net" value="Information" />
</switches>
</system.diagnostics>

"initializeData" 属性不是必需的。