Skip to content

Commit 2201bab

Browse files
authored
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
1 parent 853c36e commit 2201bab

File tree

2 files changed

+39
-63
lines changed

2 files changed

+39
-63
lines changed

src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,9 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId)
452452
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT
453453
&& error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED)
454454
{
455-
switch (error)
455+
if (IsNotFoundCase(error))
456456
{
457-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
458-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
459-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
460-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
461-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
462-
return null;
457+
return null;
463458
}
464459
if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
465460
EventLogException.Throw(error);
@@ -473,16 +468,7 @@ public static string EvtFormatMessage(EventLogHandle handle, uint msgId)
473468
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT
474469
&& error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED)
475470
{
476-
switch (error)
477-
{
478-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
479-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
480-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
481-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
482-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
483-
return null;
484-
}
485-
if (error == UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
471+
if (IsNotFoundCase(error))
486472
{
487473
return null;
488474
}
@@ -913,22 +899,18 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo
913899
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, sb, out bufferNeeded);
914900
int error = Marshal.GetLastWin32Error();
915901

916-
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
902+
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
903+
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT)
917904
{
918905
//
919906
// ERROR_EVT_UNRESOLVED_VALUE_INSERT can be returned. It means
920907
// message may have one or more unsubstituted strings. This is
921908
// not an exception, but we have no way to convey the partial
922909
// success out to enduser.
923910
//
924-
switch (error)
911+
if (IsNotFoundCase(error))
925912
{
926-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
927-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
928-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
929-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
930-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
931-
return null;
913+
return null;
932914
}
933915
if (error != (int)UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
934916
EventLogException.Throw(error);
@@ -938,16 +920,12 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo
938920
status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, bufferNeeded, sb, out bufferNeeded);
939921
error = Marshal.GetLastWin32Error();
940922

941-
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
923+
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
924+
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT)
942925
{
943-
switch (error)
926+
if (IsNotFoundCase(error))
944927
{
945-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
946-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
947-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
948-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
949-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
950-
return null;
928+
return null;
951929
}
952930
EventLogException.Throw(error);
953931
}
@@ -968,14 +946,9 @@ public static IEnumerable<string> EvtFormatMessageRenderKeywords(EventLogHandle
968946

969947
if (!status)
970948
{
971-
switch (error)
949+
if (IsNotFoundCase(error))
972950
{
973-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
974-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
975-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
976-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
977-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
978-
return keywordsList.AsReadOnly();
951+
return keywordsList.AsReadOnly();
979952
}
980953
if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
981954
EventLogException.Throw(error);
@@ -986,14 +959,9 @@ public static IEnumerable<string> EvtFormatMessageRenderKeywords(EventLogHandle
986959
error = Marshal.GetLastWin32Error();
987960
if (!status)
988961
{
989-
switch (error)
962+
if (IsNotFoundCase(error))
990963
{
991-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
992-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
993-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
994-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
995-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
996-
return keywordsList;
964+
return keywordsList;
997965
}
998966
EventLogException.Throw(error);
999967
}
@@ -1067,22 +1035,18 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev
10671035
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, sb, out bufferNeeded);
10681036
int error = Marshal.GetLastWin32Error();
10691037

1070-
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
1038+
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
1039+
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT)
10711040
{
10721041
//
10731042
// ERROR_EVT_UNRESOLVED_VALUE_INSERT can be returned. It means
10741043
// message may have one or more unsubstituted strings. This is
10751044
// not an exception, but we have no way to convey the partial
10761045
// success out to enduser.
10771046
//
1078-
switch (error)
1047+
if (IsNotFoundCase(error))
10791048
{
1080-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
1081-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
1082-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
1083-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
1084-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
1085-
return null;
1049+
return null;
10861050
}
10871051
if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
10881052
EventLogException.Throw(error);
@@ -1092,16 +1056,12 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev
10921056
status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, bufferNeeded, sb, out bufferNeeded);
10931057
error = Marshal.GetLastWin32Error();
10941058

1095-
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
1059+
if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
1060+
&& error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT)
10961061
{
1097-
switch (error)
1062+
if (IsNotFoundCase(error))
10981063
{
1099-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
1100-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
1101-
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
1102-
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
1103-
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
1104-
return null;
1064+
return null;
11051065
}
11061066
EventLogException.Throw(error);
11071067
}
@@ -1359,5 +1319,20 @@ public static string[] ConvertToStringArray(UnsafeNativeMethods.EvtVariant val,
13591319
return stringArray;
13601320
}
13611321
}
1322+
1323+
private static bool IsNotFoundCase(int error)
1324+
{
1325+
switch (error)
1326+
{
1327+
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
1328+
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
1329+
case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
1330+
case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
1331+
case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
1332+
case UnsafeNativeMethods.ERROR_RESOURCE_TYPE_NOT_FOUND:
1333+
return true;
1334+
}
1335+
return false;
1336+
}
13621337
}
13631338
}

src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal static partial class UnsafeNativeMethods
6565
// The event size is larger than the allowed maximum (64k - header).
6666
internal const int ERROR_ARITHMETIC_OVERFLOW = 0x216; // 534
6767

68+
internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x715; // 1813
6869
internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717; // 1815
6970

7071
// Event log specific codes:

0 commit comments

Comments
 (0)