Skip to content

Commit c36437b

Browse files
committed
wip commit
1 parent 73bc9db commit c36437b

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

src/WEventViewer/MainWindow.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<StackPanel Orientation="Vertical">
1313
<Menu DockPanel.Dock="Top">
1414
<MenuItem Header="_File">
15-
<MenuItem Header="_Open" Command="{Binding $parent.$parent.$parent.$parent.OpenCommand}"/>
15+
<MenuItem Header="_Open" Command="{Binding $parent.$parent.$parent.DataContext.OpenCommand}"/>
1616
</MenuItem>
1717
<MenuItem Header="_About"/>
1818
</Menu>

src/WEventViewer/MainWindow.axaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public partial class MainWindow : Window
1010
public MainWindow()
1111
{
1212
InitializeComponent();
13+
DataContext = new MainWindowViewModel();
1314
WeakReferenceMessenger.Default.Register<MainWindow, OpenLogRequest>(this, async (recpient, req) =>
1415
{
1516
var vm = new OpenLogWindowViewModel();
@@ -18,7 +19,8 @@ public MainWindow()
1819
DataContext = vm
1920
};
2021
await dlg.ShowDialog(this);
21-
22+
req.PathType = vm.PathType;
23+
req.LogName = vm.LogName;
2224
});
2325
}
2426
}

src/WEventViewer/Model/EventLogRepository.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics.Eventing.Reader;
88
using System.Threading;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using System.Collections.ObjectModel;
1011

1112
namespace WEventViewer.Model
1213
{
@@ -45,11 +46,14 @@ public static LogRecord ToLogRecord(this EventRecord record)
4546
}
4647
internal class EventLogRepository
4748
{
48-
List<LogRecord> records = [];
49-
public IEnumerable<LogRecord> Records => records;
49+
ObservableCollection<LogRecord> records = [];
50+
public ObservableCollection<LogRecord> Records => records;
51+
public void Clear()
52+
{
53+
records.Clear();
54+
}
5055
public async Task Load(string logName, PathType pathType, string? query, CancellationToken token, IProgress<long> progress)
5156
{
52-
var lst = new List<LogRecord>();
5357
using var evreader = new EventLogReader(new EventLogQuery(logName, pathType, query));
5458
long count = 0;
5559
while(!token.IsCancellationRequested)
@@ -60,23 +64,14 @@ public async Task Load(string logName, PathType pathType, string? query, Cancell
6064
{
6165
break;
6266
}
63-
lst.Add(record.ToLogRecord());
67+
records.Add(record.ToLogRecord());
6468
count++;
6569
if((count & 0xff) == 0)
6670
{
6771
progress?.Report(count);
6872
}
6973
}
7074
progress?.Report(count);
71-
while(!token.IsCancellationRequested)
72-
{
73-
var old = records;
74-
var exchanged = Interlocked.CompareExchange(ref records, lst, old);
75-
if(exchanged == old)
76-
{
77-
break;
78-
}
79-
}
8075
}
8176
}
8277
}

src/WEventViewer/ViewModel/MainWindowViewModel.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,41 @@
1313
using System.Diagnostics.Eventing.Reader;
1414
using System.Diagnostics.Eventing;
1515
using System.Collections.ObjectModel;
16+
using System.Collections.Specialized;
1617

1718
namespace WEventViewer.ViewModel
1819
{
19-
class OpenLogRequest(MainWindowViewModel vm, string logName, PathType pathType)
20+
class OpenLogRequest
2021
{
21-
public string LogName => logName;
22-
public PathType PathType => pathType;
22+
public string? LogName { get; set; }
23+
public PathType PathType { get; set; }
2324
}
2425
internal class MainWindowViewModel
2526
{
2627
EventLogRepository? _EventLogRepository;
27-
public MainWindowViewModel(): this(null)
28-
{
29-
}
30-
public MainWindowViewModel(EventLogRepository repository)
28+
public MainWindowViewModel()
3129
{
3230
_Progress = new Progress<long>(l => LogCount = l);
33-
_EventLogRepository = repository;
31+
_EventLogRepository = new EventLogRepository();
32+
_EventLogRepository.Records.CollectionChanged += Records_CollectionChanged;
3433
OpenCommand = new RelayCommand(async () =>
3534
{
36-
var ret = WeakReferenceMessenger.Default.Send<OpenLogRequest>(new OpenLogRequest(this, "", PathType.LogName));
37-
await _EventLogRepository.Load(ret.LogName, ret.PathType, null, default, _Progress);
35+
var ret = WeakReferenceMessenger.Default.Send<OpenLogRequest>(new OpenLogRequest());
36+
if(ret.LogName != null)
37+
{
38+
await _EventLogRepository.Load(ret.LogName, ret.PathType, null, default, _Progress);
39+
}
3840
});
3941
}
42+
43+
private void Records_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
44+
{
45+
if(_EventLogRepository != null)
46+
{
47+
LogCount = _EventLogRepository.Records.Count;
48+
}
49+
}
50+
4051
Progress<long> _Progress;
4152
public ICommand OpenCommand;
4253
long _LogCount;
@@ -49,6 +60,6 @@ public long LogCount
4960

5061
}
5162
}
52-
public ObservableCollection<LogRecord> LogRecords;
63+
public ObservableCollection<LogRecord> LogRecords => _EventLogRepository.Records;
5364
}
5465
}

0 commit comments

Comments
 (0)