Skip to content

Commit 5cdbe31

Browse files
committed
Merge pull request dotnet#5382 from davmason/filtering
Add filtering logic to XplatEventLogger
2 parents 23f07a5 + 75918f0 commit 5cdbe31

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/inc/clrconfigvalues.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,8 @@ RETAIL_CONFIG_DWORD_INFO(EXTERNAL_ReadyToRun, W("ReadyToRun"), 0, "Enable/disabl
10261026

10271027
#if defined(FEATURE_EVENT_TRACE) || defined(FEATURE_EVENTSOURCE_XPLAT)
10281028
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_EnableEventLog, W("EnableEventLog"), 0, "Enable/disable use of EnableEventLogging mechanism ") // Off by default
1029+
RETAIL_CONFIG_STRING_INFO(INTERNAL_EventSourceFilter, W("EventSourceFilter"), "")
1030+
RETAIL_CONFIG_STRING_INFO(INTERNAL_EventNameFilter, W("EventNameFilter"), "")
10291031
#endif //defined(FEATURE_EVENT_TRACE) || defined(FEATURE_EVENTSOURCE_XPLAT)
10301032

10311033
//

src/mscorlib/src/System/Diagnostics/Eventing/XplatEventLogger.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Text;
77
using System.Runtime.InteropServices;
8+
using System.Runtime.Versioning;
89

910
using Contract = System.Diagnostics.Contracts.Contract;
1011

@@ -15,6 +16,9 @@ namespace System.Diagnostics.Tracing
1516

1617
internal class XplatEventLogger : EventListener
1718
{
19+
private static Lazy<string> eventSourceNameFilter = new Lazy<string>(() => CompatibilitySwitch.GetValueInternal("EventSourceFilter"));
20+
private static Lazy<string> eventSourceEventFilter = new Lazy<string>(() => CompatibilitySwitch.GetValueInternal("EventNameFilter"));
21+
1822
public XplatEventLogger() {}
1923

2024
private static bool initializedPersistentListener = false;
@@ -122,12 +126,20 @@ private static string Serialize(ReadOnlyCollection<string> payloadName, ReadOnly
122126

123127
internal protected override void OnEventSourceCreated(EventSource eventSource)
124128
{
125-
EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All, null);
129+
string eventSourceFilter = eventSourceNameFilter.Value;
130+
if (String.IsNullOrEmpty(eventSourceFilter) || (eventSource.Name.IndexOf(eventSourceFilter, StringComparison.OrdinalIgnoreCase) >= 0))
131+
{
132+
EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All, null);
133+
}
126134
}
127135

128136
internal protected override void OnEventWritten(EventWrittenEventArgs eventData)
129137
{
130-
LogOnEventWritten(eventData);
138+
string eventFilter = eventSourceEventFilter.Value;
139+
if (String.IsNullOrEmpty(eventFilter) || (eventData.EventName.IndexOf(eventFilter, StringComparison.OrdinalIgnoreCase) >= 0))
140+
{
141+
LogOnEventWritten(eventData);
142+
}
131143
}
132144

133145
[System.Security.SecuritySafeCritical]

0 commit comments

Comments
 (0)