Add filtering logic to XplatEventLogger
authorDavid Mason <davmason@microsoft.com>
Tue, 31 May 2016 22:07:09 +0000 (15:07 -0700)
committerDavid Mason <davmason@microsoft.com>
Thu, 2 Jun 2016 05:04:16 +0000 (22:04 -0700)
src/inc/clrconfigvalues.h
src/mscorlib/src/System/Diagnostics/Eventing/XplatEventLogger.cs

index 98c5aa5..a673f36 100644 (file)
@@ -1027,6 +1027,8 @@ RETAIL_CONFIG_DWORD_INFO(EXTERNAL_ReadyToRun, W("ReadyToRun"), 0, "Enable/disabl
 
 #if defined(FEATURE_EVENT_TRACE) || defined(FEATURE_EVENTSOURCE_XPLAT)
 RETAIL_CONFIG_DWORD_INFO(EXTERNAL_EnableEventLog, W("EnableEventLog"), 0, "Enable/disable use of EnableEventLogging mechanism ") // Off by default 
+RETAIL_CONFIG_STRING_INFO(INTERNAL_EventSourceFilter, W("EventSourceFilter"), "")
+RETAIL_CONFIG_STRING_INFO(INTERNAL_EventNameFilter, W("EventNameFilter"), "")
 #endif //defined(FEATURE_EVENT_TRACE) || defined(FEATURE_EVENTSOURCE_XPLAT)
 
 //
index 4e380ee..d7112fc 100644 (file)
@@ -5,6 +5,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Text;
 using System.Runtime.InteropServices;
+using System.Runtime.Versioning;
 
 using Contract = System.Diagnostics.Contracts.Contract;
 
@@ -15,6 +16,9 @@ namespace System.Diagnostics.Tracing
 
     internal  class XplatEventLogger : EventListener
     {
+        private static Lazy<string> eventSourceNameFilter = new Lazy<string>(() => CompatibilitySwitch.GetValueInternal("EventSourceFilter"));
+        private static Lazy<string> eventSourceEventFilter = new Lazy<string>(() => CompatibilitySwitch.GetValueInternal("EventNameFilter"));
+        
         public XplatEventLogger() {}
 
         private static bool initializedPersistentListener = false;
@@ -122,12 +126,20 @@ namespace System.Diagnostics.Tracing
 
         internal protected  override void OnEventSourceCreated(EventSource eventSource)
         {
-            EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All, null);
+            string eventSourceFilter = eventSourceNameFilter.Value;
+            if (String.IsNullOrEmpty(eventSourceFilter) || (eventSource.Name.IndexOf(eventSourceFilter, StringComparison.OrdinalIgnoreCase) >= 0))
+            {   
+                EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All, null);
+            }
         }
 
         internal protected  override void OnEventWritten(EventWrittenEventArgs eventData)
         {
-            LogOnEventWritten(eventData);
+            string eventFilter = eventSourceEventFilter.Value;
+            if (String.IsNullOrEmpty(eventFilter) || (eventData.EventName.IndexOf(eventFilter, StringComparison.OrdinalIgnoreCase) >= 0))
+            {
+                LogOnEventWritten(eventData);
+            }
         }
 
         [System.Security.SecuritySafeCritical]