Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle special error event #57804

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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