Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -967,7 +967,13 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo
EventLogException.Throw(error);
}

int len = bufferNeeded - 1; // buffer includes null terminator

// buffer may include null terminators
int len = bufferNeeded;
while (len > 0 && buffer[len - 1] == '\0')
{
len--;
}
if (len <= 0)
return string.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Win32.SafeHandles;
using Xunit;

Expand Down Expand Up @@ -50,41 +50,41 @@ public unsafe void CanFormatMessage(uint messageId)
}
}

public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && Helpers.IsElevatedAndSupportsEventLogs;

[ConditionalFact(nameof(HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/88224", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows10Version22000OrGreater))]
[ConditionalFact(typeof(Helpers), nameof(Helpers.HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
public void CanReadAndWriteMessages()
{
string messageDllPath = Path.Combine(Path.GetDirectoryName(typeof(EventLog).Assembly.Location), "System.Diagnostics.EventLog.Messages.dll");
EventSourceCreationData log = new EventSourceCreationData($"TestEventMessageSource {Guid.NewGuid()}", "Application")
RemoteExecutor.Invoke(() =>
{
MessageResourceFile = messageDllPath
};
try
{
if (EventLog.SourceExists(log.Source))
string messageDllPath = Path.Combine(Path.GetDirectoryName(typeof(EventLog).Assembly.Location), "System.Diagnostics.EventLog.Messages.dll");
EventSourceCreationData log = new EventSourceCreationData($"TestEventMessageSource {Guid.NewGuid()}", "Application")
{
EventLog.DeleteEventSource(log.Source);
}
MessageResourceFile = messageDllPath
};
try
{
if (EventLog.SourceExists(log.Source))
{
EventLog.DeleteEventSource(log.Source);
}

EventLog.CreateEventSource(log);
string message = $"Hello {Guid.NewGuid()}";
Helpers.Retry(() => EventLog.WriteEntry(log.Source, message));
EventLog.CreateEventSource(log);
string message = $"Hello {Guid.NewGuid()}";
Helpers.Retry(() => EventLog.WriteEntry(log.Source, message));

using (EventLogReader reader = new EventLogReader(new EventLogQuery("Application", PathType.LogName, $"*[System/Provider/@Name=\"{log.Source}\"]")))
{
EventRecord evt = reader.ReadEvent();
using (EventLogReader reader = new EventLogReader(new EventLogQuery("Application", PathType.LogName, $"*[System/Provider/@Name=\"{log.Source}\"]")))
{
EventRecord evt = reader.ReadEvent();

string logMessage = evt.FormatDescription();
string logMessage = evt.FormatDescription();

Assert.Equal(message, logMessage);
Assert.Equal(message, logMessage);
}
}
}
finally
{
EventLog.DeleteEventSource(log.Source);
}
finally
{
EventLog.DeleteEventSource(log.Source);
}
}).Dispose();
}
}
}
6 changes: 4 additions & 2 deletions src/libraries/System.Diagnostics.EventLog/tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

// Implementation is not robust with respect to concurrently writing and reading log
Expand All @@ -13,8 +14,9 @@ namespace System.Diagnostics.Tests
{
internal class Helpers
{
public static bool NotElevatedAndSupportsEventLogs { get => !AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
public static bool IsElevatedAndSupportsEventLogs { get => AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && IsElevatedAndSupportsEventLogs;
public static bool NotElevatedAndSupportsEventLogs { get => !RemoteExecutor.IsSupported && SupportsEventLogs; }
public static bool IsElevatedAndSupportsEventLogs { get => RemoteExecutor.IsSupported && SupportsEventLogs; }
public static bool SupportsEventLogs { get => PlatformDetection.IsNotWindowsNanoNorServerCore && PlatformDetection.IsNotWindowsIoTCore; }

// Retry that eats exceptions: for "best effort cleanup"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableLibraryImportGenerator>true</EnableLibraryImportGenerator>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<ItemGroup>
<Compile Include="EventInstanceTests.cs" />
Expand Down