Skip to content

Commit

Permalink
handle special error event (#57804)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FatTigerWang committed Aug 24, 2021
1 parent 853c36e commit 2201bab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -913,22 +899,18 @@ 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
// message may have one or more unsubstituted strings. This is
// 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);
Expand All @@ -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);
}
Expand All @@ -968,14 +946,9 @@ public static IEnumerable<string> 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);
Expand All @@ -986,14 +959,9 @@ public static IEnumerable<string> 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);
}
Expand Down Expand Up @@ -1067,22 +1035,18 @@ 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
// message may have one or more unsubstituted strings. This is
// 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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2201bab

Please sign in to comment.