From 2201bab70988f7e856b9a3321866cda366b368fb Mon Sep 17 00:00:00 2001 From: FatTiger Date: Wed, 25 Aug 2021 00:28:21 +0800 Subject: [PATCH] handle special error event (#57804) * fix #56469 handle special error event * fix #56469 handle special error event * fix #56469 handle special error event * fix if keyword followed space code style --- .../Diagnostics/Reader/NativeWrapper.cs | 101 +++++++----------- .../Diagnostics/Reader/UnsafeNativeMethods.cs | 1 + 2 files changed, 39 insertions(+), 63 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs index 1bd7e19dc5459..ac44ce3773feb 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs @@ -452,14 +452,9 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED) { - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; + return null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -473,16 +468,7 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED) { - switch (error) - { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; - } - if (error == UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) + if (IsNotFoundCase(error)) { return null; } @@ -913,7 +899,8 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, sb, out bufferNeeded); int error = Marshal.GetLastWin32Error(); - if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) + if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT + && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { // // ERROR_EVT_UNRESOLVED_VALUE_INSERT can be returned. It means @@ -921,14 +908,9 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo // not an exception, but we have no way to convey the partial // success out to enduser. // - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; + return null; } if (error != (int)UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -938,16 +920,12 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, bufferNeeded, sb, out bufferNeeded); error = Marshal.GetLastWin32Error(); - if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) + if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT + && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; + return null; } EventLogException.Throw(error); } @@ -968,14 +946,9 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle if (!status) { - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return keywordsList.AsReadOnly(); + return keywordsList.AsReadOnly(); } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -986,14 +959,9 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle error = Marshal.GetLastWin32Error(); if (!status) { - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return keywordsList; + return keywordsList; } EventLogException.Throw(error); } @@ -1067,7 +1035,8 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, sb, out bufferNeeded); int error = Marshal.GetLastWin32Error(); - if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) + if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT + && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { // // ERROR_EVT_UNRESOLVED_VALUE_INSERT can be returned. It means @@ -1075,14 +1044,9 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev // not an exception, but we have no way to convey the partial // success out to enduser. // - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; + return null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -1092,16 +1056,12 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, bufferNeeded, sb, out bufferNeeded); error = Marshal.GetLastWin32Error(); - if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) + if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT + && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { - switch (error) + if (IsNotFoundCase(error)) { - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: - case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: - case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: - case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: - return null; + return null; } EventLogException.Throw(error); } @@ -1359,5 +1319,20 @@ public static string[] ConvertToStringArray(UnsafeNativeMethods.EvtVariant val, return stringArray; } } + + private static bool IsNotFoundCase(int error) + { + switch (error) + { + case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND: + case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND: + case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND: + case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND: + case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND: + case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: + return true; + } + return false; + } } } diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs index f1a5ef3955a1d..9dff026f9ab6d 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs @@ -65,6 +65,7 @@ internal static partial class UnsafeNativeMethods // The event size is larger than the allowed maximum (64k - header). internal const int ERROR_ARITHMETIC_OVERFLOW = 0x216; // 534 + internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x715; // 1813 internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717; // 1815 // Event log specific codes: