Skip to content

Commit

Permalink
[release/8.0-staging] Workaround bug in EvtFormatMessage (#105762)
Browse files Browse the repository at this point in the history
* Workaround bug in EvtFormatMessage

* Add issue comment for 100198

* Use stackalloc buffer

* Enable EventLog in servicing

---------

Co-authored-by: Eric StJohn <[email protected]>
  • Loading branch information
github-actions[bot] and ericstj authored Aug 12, 2024
1 parent 5944303 commit 9de0921
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>Provides the System.Diagnostics.EventLog class, which allows the applications to use the Windows event log service.

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHan
public static string EvtFormatMessage(EventLogHandle handle, uint msgId)
{
int bufferNeeded;
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, null, out bufferNeeded);
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

// ERROR_EVT_UNRESOLVED_VALUE_INSERT and its cousins are commonly returned for raw message text.
Expand Down Expand Up @@ -933,7 +934,8 @@ public static IList<object> EvtRenderBufferWithContextUserOrValues(EventLogHandl
public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag)
{
int bufferNeeded;
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, null, out bufferNeeded);
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
Expand Down Expand Up @@ -985,11 +987,12 @@ public static IEnumerable<string> EvtFormatMessageRenderKeywords(EventLogHandle
{
IntPtr buffer = IntPtr.Zero;
int bufferNeeded;
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198

try
{
List<string> keywordsList = new List<string>();
bool status = UnsafeNativeMethods.EvtFormatMessageBuffer(pmHandle, eventHandle, 0, 0, IntPtr.Zero, flag, 0, IntPtr.Zero, out bufferNeeded);
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status)
Expand Down Expand Up @@ -1071,6 +1074,7 @@ public static string EvtRenderBookmark(EventLogHandle eventHandle)
public static string EvtFormatMessageFormatDescription(EventLogHandle handle, EventLogHandle eventHandle, string[] values)
{
int bufferNeeded;
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198

UnsafeNativeMethods.EvtStringVariant[] stringVariants = new UnsafeNativeMethods.EvtStringVariant[values.Length];
for (int i = 0; i < values.Length; i++)
Expand All @@ -1079,7 +1083,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev
stringVariants[i].StringVal = values[i];
}

bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, null, out bufferNeeded);
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ internal static partial bool EvtFormatMessage(
EvtStringVariant[] values,
EvtFormatMessageFlags flags,
int bufferSize,
[Out] char[]? buffer,
Span<char> buffer,
out int bufferUsed);

[LibraryImport(Interop.Libraries.Wevtapi, EntryPoint = "EvtFormatMessage", SetLastError = true)]
Expand Down

0 comments on commit 9de0921

Please sign in to comment.