From de1ec8d6cd8abe8e0772abf2ff8233264c297f2f Mon Sep 17 00:00:00 2001 From: FatTiger Date: Fri, 20 Aug 2021 13:28:51 +0800 Subject: [PATCH 1/4] fix #56469 handle special error event --- .../Diagnostics/Reader/NativeWrapper.cs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 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 1bd7e19dc54599..9d43171b98f064 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 @@ -459,6 +459,7 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) 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 null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) @@ -480,12 +481,9 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) 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 null; } - if (error == UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) - { - return null; - } EventLogException.Throw(error); } return sb.ToString(); @@ -913,7 +911,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 @@ -928,6 +927,7 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo 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 null; } if (error != (int)UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) @@ -938,7 +938,8 @@ 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) { @@ -947,6 +948,7 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo 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 null; } EventLogException.Throw(error); @@ -975,6 +977,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle 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 keywordsList.AsReadOnly(); } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) @@ -993,6 +996,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle 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 keywordsList; } EventLogException.Throw(error); @@ -1067,7 +1071,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 @@ -1082,6 +1087,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev 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 null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) @@ -1092,7 +1098,8 @@ 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) { @@ -1101,6 +1108,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev 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 null; } EventLogException.Throw(error); From 28461d1831b7dc2ff2c914826dcf7d5384072c71 Mon Sep 17 00:00:00 2001 From: FatTiger Date: Fri, 20 Aug 2021 14:39:19 +0800 Subject: [PATCH 2/4] fix #56469 handle special error event --- .../src/System/Diagnostics/Reader/UnsafeNativeMethods.cs | 1 + 1 file changed, 1 insertion(+) 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 f1a5ef3955a1d3..c816b6d56677e9 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 @@ -66,6 +66,7 @@ internal static partial class UnsafeNativeMethods internal const int ERROR_ARITHMETIC_OVERFLOW = 0x216; // 534 internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717; // 1815 + internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x718; // 1816 // Event log specific codes: From ce41275b93fc21881b7cfa72787dbf6d2b1e20fd Mon Sep 17 00:00:00 2001 From: FatTiger Date: Sat, 21 Aug 2021 13:26:37 +0800 Subject: [PATCH 3/4] fix #56469 handle special error event --- .../Diagnostics/Reader/NativeWrapper.cs | 95 ++++++------------- .../Diagnostics/Reader/UnsafeNativeMethods.cs | 2 +- 2 files changed, 32 insertions(+), 65 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 9d43171b98f064..102c1ab1429280 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,15 +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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -474,15 +468,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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } EventLogException.Throw(error); } @@ -920,15 +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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } if (error != (int)UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -941,15 +923,9 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo 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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } EventLogException.Throw(error); } @@ -970,15 +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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return keywordsList.AsReadOnly(); + return keywordsList.AsReadOnly(); } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -989,15 +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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return keywordsList; + return keywordsList; } EventLogException.Throw(error); } @@ -1080,15 +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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) EventLogException.Throw(error); @@ -1101,15 +1059,9 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev 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: - case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND: - return null; + return null; } EventLogException.Throw(error); } @@ -1367,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 c816b6d56677e9..9dff026f9ab6d7 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,8 +65,8 @@ 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 - internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x718; // 1816 // Event log specific codes: From fdce7ddf7cd41f6c2abbad810d9973691421bdc5 Mon Sep 17 00:00:00 2001 From: FatTiger Date: Sat, 21 Aug 2021 13:49:49 +0800 Subject: [PATCH 4/4] fix if keyword followed space code style --- .../System/Diagnostics/Reader/NativeWrapper.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 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 102c1ab1429280..ac44ce3773feb0 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,7 +452,7 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; } @@ -468,7 +468,7 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId) && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; } @@ -908,7 +908,7 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo // not an exception, but we have no way to convey the partial // success out to enduser. // - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; } @@ -923,7 +923,7 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; } @@ -946,7 +946,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle if (!status) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return keywordsList.AsReadOnly(); } @@ -959,7 +959,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle error = Marshal.GetLastWin32Error(); if (!status) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return keywordsList; } @@ -1044,7 +1044,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev // not an exception, but we have no way to convey the partial // success out to enduser. // - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; } @@ -1059,7 +1059,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT) { - if(IsNotFoundCase(error)) + if (IsNotFoundCase(error)) { return null; }