Skip to content

Commit

Permalink
Updated a few accessibility modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 committed Jan 14, 2025
1 parent 3643912 commit aedbfbf
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 130 deletions.
130 changes: 64 additions & 66 deletions src/EventLogExpert.Eventing/Helpers/EventMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ internal enum EvtFormatMessageFlags
Xml
}

internal enum EvtLoginClass { EvtRpcLogin = 1 }

internal enum EvtLogPropertyId
{
CreationTime,
Expand Down Expand Up @@ -507,70 +505,6 @@ internal static string FormatMessage(EvtHandle publisherMetadataHandle, uint mes
return bufferUsed - 1 <= 0 ? string.Empty : new string(buffer[..(bufferUsed - 1)]);
}

/// <summary>Converts a buffer that was returned from <see cref="EvtRender" /> to an <see cref="EventRecord" /></summary>
/// <param name="eventBuffer">Pointer to a buffer returned from <see cref="EvtRender" /></param>
/// <param name="propertyCount">Number of properties returned from <see cref="EvtRender" /></param>
/// <returns></returns>
internal static EventRecord GetEventRecord(IntPtr eventBuffer, int propertyCount)
{
EventRecord properties = new();

for (int i = 0; i < propertyCount; i++)
{
var property = Marshal.PtrToStructure<EvtVariant>(eventBuffer + (i * Marshal.SizeOf<EvtVariant>()));
var variant = ConvertVariant(property);

// Properties are returned in the order defined in EVT_SYSTEM_PROPERTY_ID enum
switch (i)
{
case (int)EvtSystemPropertyId.ActivityId:
properties.ActivityId = (Guid?)variant;
break;
case (int)EvtSystemPropertyId.Computer:
properties.ComputerName = (string)variant!;
break;
case (int)EvtSystemPropertyId.EventId:
properties.Id = (ushort)variant!;
break;
case (int)EvtSystemPropertyId.Keywords:
properties.Keywords = (long?)(ulong?)variant;
break;
case (int)EvtSystemPropertyId.Level:
properties.Level = (byte?)variant;
break;
case (int)EvtSystemPropertyId.Channel:
properties.LogName = (string)variant!;
break;
case (int)EvtSystemPropertyId.ProcessID:
properties.ProcessId = (int?)(uint?)variant;
break;
case (int)EvtSystemPropertyId.EventRecordId:
properties.RecordId = (long?)(ulong?)variant;
break;
case (int)EvtSystemPropertyId.ProviderName:
properties.ProviderName = (string)variant!;
break;
case (int)EvtSystemPropertyId.Task:
properties.Task = (ushort?)variant;
break;
case (int)EvtSystemPropertyId.ThreadID:
properties.ThreadId = (int?)(uint?)variant;
break;
case (int)EvtSystemPropertyId.TimeCreated:
properties.TimeCreated = (DateTime)variant!;
break;
case (int)EvtSystemPropertyId.UserID:
properties.UserId = (SecurityIdentifier?)variant;
break;
case (int)EvtSystemPropertyId.Version:
properties.Version = (byte?)variant;
break;
}
}

return properties;
}

internal static object GetObjectArrayProperty(
EvtHandle array,
int index,
Expand Down Expand Up @@ -816,4 +750,68 @@ internal static void ThrowEventLogException(int error)
throw new Exception(message);
}
}

/// <summary>Converts a buffer that was returned from <see cref="EvtRender" /> to an <see cref="EventRecord" /></summary>
/// <param name="eventBuffer">Pointer to a buffer returned from <see cref="EvtRender" /></param>
/// <param name="propertyCount">Number of properties returned from <see cref="EvtRender" /></param>
/// <returns></returns>
private static EventRecord GetEventRecord(IntPtr eventBuffer, int propertyCount)
{
EventRecord properties = new();

for (int i = 0; i < propertyCount; i++)
{
var property = Marshal.PtrToStructure<EvtVariant>(eventBuffer + (i * Marshal.SizeOf<EvtVariant>()));
var variant = ConvertVariant(property);

// Properties are returned in the order defined in EVT_SYSTEM_PROPERTY_ID enum
switch (i)
{
case (int)EvtSystemPropertyId.ActivityId:
properties.ActivityId = (Guid?)variant;
break;
case (int)EvtSystemPropertyId.Computer:
properties.ComputerName = (string)variant!;
break;
case (int)EvtSystemPropertyId.EventId:
properties.Id = (ushort)variant!;
break;
case (int)EvtSystemPropertyId.Keywords:
properties.Keywords = (long?)(ulong?)variant;
break;
case (int)EvtSystemPropertyId.Level:
properties.Level = (byte?)variant;
break;
case (int)EvtSystemPropertyId.Channel:
properties.LogName = (string)variant!;
break;
case (int)EvtSystemPropertyId.ProcessID:
properties.ProcessId = (int?)(uint?)variant;
break;
case (int)EvtSystemPropertyId.EventRecordId:
properties.RecordId = (long?)(ulong?)variant;
break;
case (int)EvtSystemPropertyId.ProviderName:
properties.ProviderName = (string)variant!;
break;
case (int)EvtSystemPropertyId.Task:
properties.Task = (ushort?)variant;
break;
case (int)EvtSystemPropertyId.ThreadID:
properties.ThreadId = (int?)(uint?)variant;
break;
case (int)EvtSystemPropertyId.TimeCreated:
properties.TimeCreated = (DateTime)variant!;
break;
case (int)EvtSystemPropertyId.UserID:
properties.UserId = (SecurityIdentifier?)variant;
break;
case (int)EvtSystemPropertyId.Version:
properties.Version = (byte?)variant;
break;
}
}

return properties;
}
}
2 changes: 1 addition & 1 deletion src/EventLogExpert.Eventing/Helpers/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal enum LoadLibraryFlags : uint
LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
}

internal sealed partial class NativeMethods
internal static partial class NativeMethods
{
internal const int RT_MESSAGETABLE = 11;

Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert.Eventing/Models/EventRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public sealed record EventRecord

public string? Xml { get; set; }

public string? Error { get; set; }
public string? Error { get; init; }

public bool IsSuccess => string.IsNullOrEmpty(Error);
}
16 changes: 0 additions & 16 deletions src/EventLogExpert.Eventing/Models/EvtRpcLogin.cs

This file was deleted.

41 changes: 20 additions & 21 deletions src/EventLogExpert.Eventing/Providers/RegistryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ namespace EventLogExpert.Eventing.Providers;

public partial class RegistryProvider(string? computerName, ITraceLogger? logger = null)
{
private readonly string? _computerName = computerName;
private readonly ITraceLogger? _logger = logger;

public string? ComputerName { get; } = computerName;

/// <summary>sounds Returns the file paths for the message files for this provider.</summary>
public IEnumerable<string> GetMessageFilesForLegacyProvider(string providerName)
{
_logger?.Trace($"GetLegacyProviderFiles called for provider {providerName} on computer {ComputerName}");
_logger?.Trace($"GetLegacyProviderFiles called for provider {providerName} on computer {_computerName}");

var hklm = string.IsNullOrEmpty(ComputerName)
var hklm = string.IsNullOrEmpty(_computerName)
? Registry.LocalMachine
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ComputerName);
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, _computerName);

var eventLogKey = hklm.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog") ??
throw new OpenEventLogRegistryKeyFailedException(ComputerName ?? string.Empty);
throw new OpenEventLogRegistryKeyFailedException(_computerName ?? string.Empty);

foreach (var logSubKeyName in eventLogKey.GetSubKeyNames())
{
Expand Down Expand Up @@ -77,24 +76,12 @@ public IEnumerable<string> GetMessageFilesForLegacyProvider(string providerName)
return [];
}

public string? GetSystemRoot()
{
var hklm = string.IsNullOrEmpty(ComputerName)
? Registry.LocalMachine
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ComputerName);

var currentVersion = hklm.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
var systemRoot = currentVersion?.GetValue("SystemRoot") as string;

return systemRoot;
}

[GeneratedRegex("^[A-Z]:")]
private static partial Regex ConvertRootPath();

private IEnumerable<string> GetExpandedFilePaths(IEnumerable<string> paths)
{
if (string.IsNullOrEmpty(ComputerName))
if (string.IsNullOrEmpty(_computerName))
{
// For local computer, do it the easy way
return paths.Select(Environment.ExpandEnvironmentVariables);
Expand All @@ -104,7 +91,7 @@ private IEnumerable<string> GetExpandedFilePaths(IEnumerable<string> paths)
// TODO: Support variables other than SystemRoot?
var systemRoot = GetSystemRoot() ??
throw new ExpandFilePathsFailedException(
$"Could not get SystemRoot from remote registry: {ComputerName}");
$"Could not get SystemRoot from remote registry: {_computerName}");

paths = paths.Select(p =>
{
Expand All @@ -116,7 +103,7 @@ private IEnumerable<string> GetExpandedFilePaths(IEnumerable<string> paths)

if (match.Success)
{
newPath = $@"\\{ComputerName}\{match.Value[0]}${newPath[2..]}";
newPath = $@"\\{_computerName}\{match.Value[0]}${newPath[2..]}";
}

return newPath;
Expand All @@ -125,6 +112,18 @@ private IEnumerable<string> GetExpandedFilePaths(IEnumerable<string> paths)
return paths;
}

private string? GetSystemRoot()
{
var hklm = string.IsNullOrEmpty(_computerName)
? Registry.LocalMachine
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, _computerName);

var currentVersion = hklm.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
var systemRoot = currentVersion?.GetValue("SystemRoot") as string;

return systemRoot;
}

private class ExpandFilePathsFailedException(string msg) : Exception(msg) {}

private class OpenEventLogRegistryKeyFailedException(string msg) : Exception(msg) {}
Expand Down
6 changes: 3 additions & 3 deletions src/EventLogExpert.UI/Models/FilterGroupModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public sealed record FilterGroupModel
[JsonIgnore]
public FilterGroupId Id { get; } = FilterGroupId.Create();

public string Name { get; set; } = "New Filter Section\\New Filter Group";
public string Name { get; init; } = "New Filter Section\\New Filter Group";

[JsonIgnore]
public string DisplayName => Name.Split('\\').Last();

public IEnumerable<FilterModel> Filters { get; set; } = [];
public IEnumerable<FilterModel> Filters { get; init; } = [];

[JsonIgnore]
public bool IsEditing { get; set; }
public bool IsEditing { get; init; }
}
2 changes: 1 addition & 1 deletion src/EventLogExpert.UI/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal enum RestartFlags
RESTART_NO_REBOOT = 8
}

internal sealed partial class NativeMethods
internal static partial class NativeMethods
{
// https://learn.microsoft.com/en-us/windows/msix/non-store-developer-updates
/// <summary>Registers the active instance of an application for restart.</summary>
Expand Down
4 changes: 2 additions & 2 deletions src/EventLogExpert.UI/Services/AlertDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public interface IAlertDialogService

Task<bool> ShowAlert(string title, string message, string accept, string cancel);

Task<string> DisplayPrompt(string title, string mesage);
Task<string> DisplayPrompt(string title, string message);

Task<string> DisplayPrompt(string title, string mesage, string initialValue);
Task<string> DisplayPrompt(string title, string message, string initialValue);
}

public sealed class AlertDialogService(
Expand Down
3 changes: 1 addition & 2 deletions src/EventLogExpert.UI/Store/EventLog/EventLogEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,10 @@ private static ImmutableDictionary<string, EventLogData> DistributeEventsToManyL
IEnumerable<DisplayEventModel> eventsToDistribute)
{
var newLogs = logsToUpdate;
var events = eventsToDistribute;

foreach (var log in logsToUpdate.Values)
{
var newEventsForThisLog = events.Where(e => e.OwningLog == log.Name);
var newEventsForThisLog = eventsToDistribute.Where(e => e.OwningLog == log.Name);

if (newEventsForThisLog.Any()) { continue; }

Expand Down
32 changes: 15 additions & 17 deletions src/EventLogExpert.UI/Store/EventLog/LiveLogWatcherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ public void AddLog(string logName, string? bookmark)
}
}

public bool IsWatching()
{
using var scope = _watchersLock.EnterScope();

return _watchers.Keys.Count > 0;
}

public void RemoveAll()
{
using var scope = _watchersLock.EnterScope();
Expand All @@ -105,23 +98,20 @@ public void RemoveLog(string logName)
StopWatching(logName);
}

public void StartWatching()
private bool IsWatching()
{
using var scope = _watchersLock.EnterScope();

foreach (var logName in _logsToWatch)
{
StartWatching(logName);
}
return _watchers.Keys.Count > 0;
}

public void StopWatching()
private void StartWatching()
{
using var scope = _watchersLock.EnterScope();

foreach (var logName in _watchers.Keys)
foreach (var logName in _logsToWatch)
{
StopWatching(logName);
StartWatching(logName);
}
}

Expand All @@ -145,8 +135,6 @@ private void StartWatching(string logName)
var eventResolver = serviceScope.ServiceProvider.GetService<IEventResolver>();

_debugLogger.Trace("EventRecordWritten callback was called.");
// TODO: Not sure if we need to save current bookmark unless we plan on stopping and starting the watcher
//_bookmarks[logName] = eventArgs.Bookmark;

if (eventResolver is null)
{
Expand All @@ -172,6 +160,16 @@ private void StartWatching(string logName)
});
}

private void StopWatching()
{
using var scope = _watchersLock.EnterScope();

foreach (var logName in _watchers.Keys)
{
StopWatching(logName);
}
}

private void StopWatching(string logName)
{
using var scope = _watchersLock.EnterScope();
Expand Down

0 comments on commit aedbfbf

Please sign in to comment.