Skip to content

Commit

Permalink
Remove InvalidOperationException when reading system properties from …
Browse files Browse the repository at this point in the history
…EventLogRecords (#72913)
  • Loading branch information
ericstj committed Jul 27, 2022
1 parent 56a1cf4 commit 96b4b53
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +57,7 @@ internal void PrepareSystemData()
{
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ public static object EvtGetLogInfo(EventLogHandle handle, UnsafeNativeMethods.Ev
}
}

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;
Expand All @@ -810,9 +810,6 @@ public static void EvtRenderBufferWithContextSystem(EventLogHandle contextHandle
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++)
Expand Down Expand Up @@ -874,6 +871,9 @@ public static void EvtRenderBufferWithContextSystem(EventLogHandle contextHandle
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)));
}
Expand Down

0 comments on commit 96b4b53

Please sign in to comment.