You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 4.8应用DiagnosticSourceEventSource参数不匹配致重复日志及性能下降

.NET Framework 4.8下DiagnosticSourceEventSource参数不匹配导致调试性能下降问题

初始现象

在基于.NET 4.8的ASP.NET Web应用(重度使用Autofac)中出现严重性能下降,同时在VS2020的Debug Output中,每个HTTP请求都会生成上千条重复警告:

The parameters to the Event method do not match the parameters to the WriteEvent method. This may cause the event to be displayed incorrectly.

  • 该问题并非ASP.NET独有,Windows WPF应用中也会出现
  • 尝试禁用Task Parallel Library的ETW EventSource后无效果
  • 无关联异常,仅在调试器下影响性能,捕获性能数据时问题消失

问题定位(更新1)

通过单步调试,发现警告来自Autofac(v6.2)解析实例时的DiagnosticListener日志逻辑,调用堆栈如下:

EventSource.LogEventArgsMismatches() at EventSource.cs:line 1,036
EventSource.WriteEventVarargs() at EventSource.cs:line 935
EventSource.WriteEvent()
DiagnosticSourceEventSource.Event()
DiagnosticSourceEventSource.FilterAndTransform.<>c__DisplayClass2_1.<.ctor>b__2()
DiagnosticSourceEventSource.CallbackObserver<KeyValuePair<string, object>>.OnNext()
DiagnosticListener.Write()
DiagnosticSourceExtensions.OperationStart()
ResolveOperation.ExecuteOperation()
ResolveOperation.Execute()

当Autofac通过IContainer.DiagnosticSource记录解析调用时,若DiagnosticListener被订阅则会触发警告。触发订阅的堆栈如下:

DiagnosticListener.SubscribeInternal() at DiagnosticListener.cs:line 179
DiagnosticListener.Subscribe() at DiagnosticListener.cs:line 49
HttpHandlerDiagnosticListener.Subscribe()
DiagnosticSourceEventSource.FilterAndTransform.<>c__DisplayClass2_0.<.ctor>b__0()
DiagnosticSourceEventSource.CallbackObserver.OnNext() at DiagnosticSourceEventSource.cs:line 618
DiagnosticListener.AllListenerObservable.Subscribe()
new DiagnosticSourceEventSource.FilterAndTransform() at DiagnosticSourceEventSource.cs:line 316
DiagnosticSourceEventSource.FilterAndTransform.CreateFilterAndTransformList() at DiagnosticSourceEventSource.cs:line 216
DiagnosticSourceEventSource.OnEventCommand() at DiagnosticSourceEventSource.cs:line 155
EventSource.DoCommand() at EventSource.cs:line 1,462
EventSource.SendCommand() at EventSource.cs:line 1,383
EventSource.OverideEventProvider.OnControllerCommand() at EventSource.cs:line 2,950
EventProvider.EtwEnableCallBack() at EventProvider.cs:line 205

疑问:是什么触发了EtwEnableCallback,是否有相关配置?

根因确认(更新2)

进一步调试发现,这是DiagnosticSourceEventSource的bug,而非Autofac问题——所有经过DiagnosticSourceEventSource的事件都会生成相同警告,例如HttpClientHandler的调用堆栈:

EventSource.WriteEventVarargs() at EventSource.cs:line 936
EventSource.WriteEvent() at EventSource.cs:line 696
DiagnosticSourceEventSource.Event()
DiagnosticSourceEventSource.FilterAndTransform.<>c__DisplayClass2_1.<.ctor>b__2()
DiagnosticSourceEventSource.CallbackObserver<KeyValuePair<string, object>>.OnNext()
DiagnosticListener.Write()
HttpHandlerDiagnosticListener.RaiseResponseEvent()
HttpHandlerDiagnosticListener.HttpWebRequestArrayList.RemoveAt()
Connection.ReadStartNextRequest()
ConnectStream.CallDone()
ConnectStream.EndReadWithoutValidation()
ConnectStream.EndRead()
HttpClientHandler.WebExceptionWrapperStream.EndRead()
StreamToStreamCopy.BufferReadCallback()
LazyAsyncResult.Complete()
ContextAwareResult.CompleteCallback()
ExecutionContext.RunInternal()
ExecutionContext.Run()
ExecutionContext.Run()
ContextAwareResult.Complete()
LazyAsyncResult.ProtectedInvokeCallback()
BaseOverlappedAsyncResult.CompletionPortCallback()
_IOCompletionCallback.PerformIOCompletionCallback()
[Native to Managed Transition]

注:Autofac是项目中唯一依赖System.Diagnostics.DiagnosticSource的包(已通过project.assets.json验证)。

目前找到的临时解决方案是通过反射释放DiagnosticSourceEventSource的EventSource实例,但该方法存在未知副作用:

var diagnosticsLib = Assembly.Load("System.Diagnostics.DiagnosticSource, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
var diagnosticSourceEventSourceType = diagnosticsLib.GetType("System.Diagnostics.DiagnosticSourceEventSource");
object diagnosticSourceEventSource = diagnosticSourceEventSourceType.InvokeMember("Logger", BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public, null, null, null);
        
if (diagnosticSourceEventSource is IDisposable disposable)
    disposable.Dispose();   

已知修复情况(更新3)

该问题在.NET官方仓库已有相关记录,且已在.NET 5的System.Diagnostics.Tracing.EventSource中修复,但由于.NET Framework中该类属于mscorlib.dll,无法通过NuGet包更新,且修复未移植到.NET Framework,导致无法直接解决。

内容的提问来源于stack exchange,提问作者karmasponge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 07:09:55