Remove InvalidOperationException when reading system properties from EventLogRecords...
authorEric StJohn <ericstj@microsoft.com>
Wed, 27 Jul 2022 15:30:43 +0000 (08:30 -0700)
committerGitHub <noreply@github.com>
Wed, 27 Jul 2022 15:30:43 +0000 (09:30 -0600)
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs

index 37f50c2..bb7563f 100644 (file)
@@ -9,7 +9,6 @@ namespace System.Diagnostics.Eventing.Reader
 {
     public class EventLogRecord : EventRecord
     {
-        private const int SYSTEM_PROPERTY_COUNT = 18;
         private readonly EventLogSession _session;
 
         private readonly NativeWrapper.SystemProperties _systemProperties;
@@ -58,7 +57,7 @@ namespace System.Diagnostics.Eventing.Reader
             {
                 if (_systemProperties.filled == false)
                 {
-                    NativeWrapper.EvtRenderBufferWithContextSystem(_session.renderContextHandleSystem, Handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues, _systemProperties, SYSTEM_PROPERTY_COUNT);
+                    NativeWrapper.EvtRenderBufferWithContextSystem(_session.renderContextHandleSystem, Handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues, _systemProperties);
                     _systemProperties.filled = true;
                 }
             }
index e58e001..7f1fb58 100644 (file)
@@ -787,7 +787,7 @@ namespace System.Diagnostics.Eventing.Reader
             }
         }
 
-        public static void EvtRenderBufferWithContextSystem(EventLogHandle contextHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtRenderFlags flag, SystemProperties systemProperties, int SYSTEM_PROPERTY_COUNT)
+        public static void EvtRenderBufferWithContextSystem(EventLogHandle contextHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtRenderFlags flag, SystemProperties systemProperties)
         {
             IntPtr buffer = IntPtr.Zero;
             IntPtr pointer = IntPtr.Zero;
@@ -810,9 +810,6 @@ namespace System.Diagnostics.Eventing.Reader
                 if (!status)
                     EventLogException.Throw(win32Error);
 
-                if (propCount != SYSTEM_PROPERTY_COUNT)
-                    throw new InvalidOperationException("We do not have " + SYSTEM_PROPERTY_COUNT + " variants given for the UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues flag. (System Properties)");
-
                 pointer = buffer;
                 // Read each Variant structure
                 for (int i = 0; i < propCount; i++)
@@ -874,6 +871,9 @@ namespace System.Diagnostics.Eventing.Reader
                         case (int)UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemVersion:
                             systemProperties.Version = (byte?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeByte);
                             break;
+                        default:
+                            Debug.Fail($"Do not understand EVT_SYSTEM_PROPERTY_ID {i}.  A new case is needed.");
+                            break;
                     }
                     pointer = new IntPtr(((long)pointer + Marshal.SizeOf(varVal)));
                 }