Skip to content

Commit

Permalink
Removed direct XML references and moved XML resolving to event reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 authored and bill-long committed Dec 2, 2024
1 parent 18ecce6 commit 4b18bea
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private class UnitTestEventResolver : EventResolverBase, IEventResolver
internal UnitTestEventResolver(List<ProviderDetails> providerDetailsList) =>
providerDetailsList.ForEach(p => providerDetails.TryAdd(p.ProviderName, p));

public void ResolveProviderDetails(EventRecord eventRecord, string owningLogName)
public void ResolveProviderDetails(EventRecord eventRecord)
{
if (providerDetails.ContainsKey(eventRecord.ProviderName))
{
Expand Down Expand Up @@ -80,7 +80,7 @@ public void EventResolver_MSExchangeReplId4114_ShouldResolve()
};

var resolver = new UnitTestEventResolver([providerDetails]);
var @event = resolver.ResolveEvent(eventRecord, eventRecord.LogName);
var @event = resolver.ResolveEvent(eventRecord);

var expectedDescription = "Database redundancy health check passed.\r\nDatabase copy: SERVER1\r\nRedundancy count: 4\r\nIsSuppressed: False\r\n\r\nErrors:\r\nLots of copy status text";

Expand All @@ -100,7 +100,7 @@ public void PerfTest()
{
foreach (var record in er)
{
resolver.ResolveProviderDetails(record, "Test");
resolver.ResolveProviderDetails(record);
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public void PerfTest2()

foreach (var record in eventRecords)
{
resolver.ResolveProviderDetails(record, "Test");
resolver.ResolveProviderDetails(record);
}

Debug.WriteLine("Resolving events took " + Stopwatch.GetElapsedTime(sw));
Expand Down Expand Up @@ -168,7 +168,7 @@ public void Test1()

foreach (var r in resolvers)
{
var @event = r.ResolveEvent(record, "Test");
var @event = r.ResolveEvent(record);

var description = @event.Description;
var keywords = @event.KeywordsDisplayNames;
Expand Down
6 changes: 3 additions & 3 deletions src/EventLogExpert.Eventing/Helpers/EventMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ internal static int GetObjectArraySize(EvtHandle array)
return size;
}

internal static EventRecord RenderEvent(EvtHandle eventHandle, EvtRenderFlags flag)
internal static EventRecord RenderEvent(EvtHandle eventHandle)
{
IntPtr buffer = IntPtr.Zero;

Expand All @@ -631,7 +631,7 @@ internal static EventRecord RenderEvent(EvtHandle eventHandle, EvtRenderFlags fl
bool success = EvtRender(
EventLogSession.GlobalSession.SystemRenderContext,
eventHandle,
flag,
EvtRenderFlags.EventValues,
0,
buffer,
out int bufferUsed,
Expand All @@ -649,7 +649,7 @@ internal static EventRecord RenderEvent(EvtHandle eventHandle, EvtRenderFlags fl
success = EvtRender(
EventLogSession.GlobalSession.SystemRenderContext,
eventHandle,
flag,
EvtRenderFlags.EventValues,
bufferUsed,
buffer,
out bufferUsed,
Expand Down
28 changes: 0 additions & 28 deletions src/EventLogExpert.Eventing/Models/DisplayEventModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// // Licensed under the MIT License.

using EventLogExpert.Eventing.Helpers;
using EventLogExpert.Eventing.Readers;
using System.Security.Principal;

namespace EventLogExpert.Eventing.Models;
Expand Down Expand Up @@ -39,31 +38,4 @@ public sealed record DisplayEventModel(
public DateTime TimeCreated { get; init; }

public SecurityIdentifier? UserId { get; init; }

public string Xml
{
get
{
using EvtHandle handle = EventMethods.EvtQuery(
EventLogSession.GlobalSession.Handle,
OwningLog,
$"*[System/EventRecordID=\"{RecordId}\"]",
PathType);

if (handle.IsInvalid) { return string.Empty; }

var buffer = new IntPtr[1];
int count = 0;

bool success = EventMethods.EvtNext(handle, buffer.Length, buffer, 0, 0, ref count);

if (!success) { return string.Empty; }

using var eventHandle = new EvtHandle(buffer[0]);

if (eventHandle.IsInvalid) { return string.Empty; }

return EventMethods.RenderEventXml(eventHandle) ?? string.Empty;
}
}
}
26 changes: 25 additions & 1 deletion src/EventLogExpert.Eventing/Readers/EventLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public sealed partial class EventLogReader(string path, PathType pathType) : IDi

public string? LastBookmark { get; private set; }

public static string GetXml(DisplayEventModel record)
{
using EvtHandle handle = EventMethods.EvtQuery(
EventLogSession.GlobalSession.Handle,
record.OwningLog,
$"*[System[EventRecordID='{record.RecordId}']]",
record.PathType);

if (handle.IsInvalid) { return string.Empty; }

var buffer = new IntPtr[1];
int count = 0;

bool success = EventMethods.EvtNext(handle, buffer.Length, buffer, 0, 0, ref count);

if (!success) { return string.Empty; }

using EvtHandle eventHandle = new(buffer[0]);

if (eventHandle.IsInvalid) { return string.Empty; }

return EventMethods.RenderEventXml(eventHandle) ?? string.Empty;
}

public void Dispose()
{
Dispose(disposing: true);
Expand Down Expand Up @@ -55,7 +79,7 @@ public bool TryGetEvents(out EventRecord[] events, int batchSize = 64)

try
{
events[i] = EventMethods.RenderEvent(eventHandle, EvtRenderFlags.EventValues);
events[i] = EventMethods.RenderEvent(eventHandle);
events[i].Properties = EventMethods.RenderEventProperties(eventHandle);
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert.Eventing/Readers/EventLogWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void ProcessNewEvents(object? state, bool timedOut)

try
{
@event = EventMethods.RenderEvent(eventHandle, EvtRenderFlags.EventValues);
@event = EventMethods.RenderEvent(eventHandle);
@event.Properties = EventMethods.RenderEventProperties(eventHandle);
}
catch (Exception ex)
Expand Down
3 changes: 1 addition & 2 deletions src/EventLogExpert.UI/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public enum FilterCategory
[EnumMember(Value = "Keywords")] KeywordsDisplayNames,
Source,
[EnumMember(Value = "Task Category")] TaskCategory,
Description,
Xml
Description
}

public enum FilterEvaluator
Expand Down
31 changes: 10 additions & 21 deletions src/EventLogExpert.UI/Models/EventLogData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@ public readonly record struct EventLogData(
public EventLogId Id { get; } = EventLogId.Create();

/// <summary>Gets a distinct list of values for the specified category.</summary>
public IEnumerable<string> GetCategoryValues(FilterCategory category)
{
switch (category)
public IEnumerable<string> GetCategoryValues(FilterCategory category) =>
category switch
{
case FilterCategory.Id:
return Events.Select(e => e.Id.ToString()).Distinct();
case FilterCategory.ActivityId:
return Events.Select(e => e.ActivityId?.ToString() ?? string.Empty).Distinct();
case FilterCategory.Level:
return Enum.GetNames<SeverityLevel>();
case FilterCategory.KeywordsDisplayNames:
return Events.SelectMany(e => e.KeywordsDisplayNames).Distinct();
case FilterCategory.Source:
return Events.Select(e => e.Source).Distinct();
case FilterCategory.TaskCategory:
return Events.Select(e => e.TaskCategory).Distinct();
case FilterCategory.Xml:
case FilterCategory.Description:
default:
return [];
}
}
FilterCategory.Id => Events.Select(e => e.Id.ToString()).Distinct(),
FilterCategory.ActivityId => Events.Select(e => e.ActivityId?.ToString() ?? string.Empty).Distinct(),
FilterCategory.Level => Enum.GetNames<SeverityLevel>(),
FilterCategory.KeywordsDisplayNames => Events.SelectMany(e => e.KeywordsDisplayNames).Distinct(),
FilterCategory.Source => Events.Select(e => e.Source).Distinct(),
FilterCategory.TaskCategory => Events.Select(e => e.TaskCategory).Distinct(),
_ => [],
};
}
5 changes: 3 additions & 2 deletions src/EventLogExpert/Components/DetailsPane.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using System.Xml.Linq
@using EventLogExpert.Eventing.Readers
@inherits FluxorComponent

<div data-toggle="@IsVisible" hidden="@(SelectedEvent is null)" id="details-pane">
Expand Down Expand Up @@ -46,9 +47,9 @@
</div>

<p class="details-xml" data-toggle="@_isXmlVisible.ToString().ToLower()">
@if (!string.IsNullOrEmpty(SelectedEvent.Xml))
@if (!string.IsNullOrEmpty(_selectedXml))
{
@XElement.Parse(SelectedEvent.Xml)
@XElement.Parse(_selectedXml)
}
else
{
Expand Down
11 changes: 8 additions & 3 deletions src/EventLogExpert/Components/DetailsPane.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// // Licensed under the MIT License.

using EventLogExpert.Eventing.Models;
using EventLogExpert.Eventing.Readers;
using EventLogExpert.Services;
using EventLogExpert.UI;
using EventLogExpert.UI.Store.EventLog;
Expand All @@ -18,6 +19,7 @@ public sealed partial class DetailsPane
private bool _hasOpened = false;
private bool _isVisible = false;
private bool _isXmlVisible = false;
private string _selectedXml = string.Empty;

[Inject] private IClipboardService ClipboardService { get; init; } = null!;

Expand Down Expand Up @@ -49,11 +51,14 @@ protected override void OnInitialized()
{
SelectedEvent = selectedEvents.LastOrDefault();

if (SelectedEvent is not null &&
(!_hasOpened || SettingsState.Value.Config.ShowDisplayPaneOnSelectionChange))
if (SelectedEvent is null ||
(_hasOpened && !SettingsState.Value.Config.ShowDisplayPaneOnSelectionChange))
{
_isVisible = true;
return;
}

_selectedXml = EventLogReader.GetXml(SelectedEvent);
_isVisible = true;
};

base.OnInitialized();
Expand Down
15 changes: 11 additions & 4 deletions src/EventLogExpert/Services/ClipboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// // Licensed under the MIT License.

using EventLogExpert.Eventing.Models;
using EventLogExpert.Eventing.Readers;
using EventLogExpert.UI;
using EventLogExpert.UI.Store.EventLog;
using EventLogExpert.UI.Store.EventTable;
Expand Down Expand Up @@ -60,6 +61,8 @@ public void CopySelectedEvent(CopyType? copyType = null)

private string GetFormattedEvent(CopyType? copyType, DisplayEventModel @event)
{
string xml;

switch (copyType ?? _settingsState.Value.Config.CopyType)
{
case CopyType.Default:
Expand Down Expand Up @@ -120,9 +123,11 @@ private string GetFormattedEvent(CopyType? copyType, DisplayEventModel @event)

return simpleEvent.ToString();
case CopyType.Xml:
return string.IsNullOrEmpty(@event.Xml) ?
xml = EventLogReader.GetXml(@event);

return string.IsNullOrEmpty(xml) ?
string.Empty :
XElement.Parse(@event.Xml).ToString();
XElement.Parse(xml).ToString();
case CopyType.Full:
default:
StringBuilder fullEvent = new();
Expand All @@ -140,9 +145,11 @@ private string GetFormattedEvent(CopyType? copyType, DisplayEventModel @event)
fullEvent.AppendLine(@event.Description);
fullEvent.AppendLine("Event Xml:");

if (!string.IsNullOrEmpty(@event.Xml))
xml = EventLogReader.GetXml(@event);

if (!string.IsNullOrEmpty(xml))
{
fullEvent.AppendLine(XElement.Parse(@event.Xml).ToString());
fullEvent.AppendLine(XElement.Parse(xml).ToString());
}

return fullEvent.ToString();
Expand Down
4 changes: 2 additions & 2 deletions src/EventLogExpert/Shared/Components/ContextMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ul>
@foreach (FilterCategory item in Enum.GetValues(typeof(FilterCategory)))
{
@if (item is FilterCategory.Description or FilterCategory.Xml) { continue; }
@if (item is FilterCategory.Description) { continue; }

<li @onclick="() => FilterEvent(item)">@item.ToFullString()</li>
}
Expand All @@ -25,7 +25,7 @@
<ul>
@foreach (FilterCategory item in Enum.GetValues(typeof(FilterCategory)))
{
@if (item is FilterCategory.Description or FilterCategory.Xml) { continue; }
@if (item is FilterCategory.Description) { continue; }

<li @onclick="() => FilterEvent(item, true)">@item.ToFullString()</li>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private List<string> Items
return EventLogState.Value.ActiveLogs.Values
.SelectMany(log => log.GetCategoryValues(FilterCategory.TaskCategory))
.Distinct().Order().ToList();
case FilterCategory.Xml:
case FilterCategory.Description:
default:
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private List<string> Items
return EventLogState.Value.ActiveLogs.Values
.SelectMany(log => log.GetCategoryValues(FilterCategory.TaskCategory))
.Distinct().Order().ToList();
case FilterCategory.Xml:
case FilterCategory.Description:
default:
return [];
Expand Down

0 comments on commit 4b18bea

Please sign in to comment.