Skip to content

Commit af72764

Browse files
authored
Fix KeyNotFoundException in HttpRequestLatencyListener.OnEventWritten (#6823)
1 parent b56aec4 commit af72764

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpMockProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static Mock<ILatencyContext> GetLatencyContext()
4040
return lc;
4141
}
4242

43-
public class MockEventSource : EventSource
43+
public class MockEventSource() : EventSource(throwOnEventWriteErrors: true)
4444
{
4545
public int OnEventInvoked;
4646

test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpRequestLatencyListenerTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.Tracing;
45
using System.Linq;
56
using Microsoft.Extensions.Diagnostics.Latency;
67
using Microsoft.Extensions.Http.Latency.Internal;
@@ -84,8 +85,16 @@ public void HttpRequestLatencyListener_OnEventSourceCreated_NonHttpSources()
8485
listener.OnEventSourceCreated("test", es);
8586
Assert.Equal(0, es.OnEventInvoked);
8687
Assert.False(es.IsEnabled());
88+
89+
using var dummyListener = new DummyListener();
90+
dummyListener.EnableEvents(es, EventLevel.LogAlways);
91+
92+
// EventSource seems to send the event to all listeners, even those that didn't enable the EventSource at all!
93+
es.Write("Dummy");
8794
}
8895

96+
private sealed class DummyListener : EventListener;
97+
8998
[Fact]
9099
public void HttpRequestLatencyListener_OnEventSourceCreated_HttpSources()
91100
{

0 commit comments

Comments
 (0)