Skip to content

Commit 076289c

Browse files
committed
add error output window
1 parent 2625468 commit 076289c

11 files changed

+188
-23
lines changed

src/WEventViewer/App.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
<Application.Styles>
88
<FluentTheme />
9+
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
910
</Application.Styles>
1011
</Application>

src/WEventViewer/ErrorWindow.axaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6+
xmlns:vm="using:WEventViewer.ViewModel"
7+
x:Class="WEventViewer.ErrorWindow"
8+
x:DataType="vm:ErrorWindowViewModel"
9+
Title="ErrorWindow">
10+
<Design.DataContext>
11+
<vm:ErrorWindowViewModel></vm:ErrorWindowViewModel>
12+
</Design.DataContext>
13+
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
14+
<ScrollViewer AllowAutoHide="True" VerticalScrollBarVisibility="Auto">
15+
<TextBlock Text="{Binding Message}" TextWrapping="WrapWithOverflow"/>
16+
</ScrollViewer>
17+
<Button Content="Close"/>
18+
</StackPanel>
19+
</Window>

src/WEventViewer/ErrorWindow.axaml.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Avalonia.Controls;
2+
using CommunityToolkit.Mvvm.Messaging;
3+
using WEventViewer.ViewModel;
4+
5+
namespace WEventViewer
6+
{
7+
public partial class ErrorWindow : Window
8+
{
9+
10+
public ErrorWindow()
11+
{
12+
InitializeComponent();
13+
WeakReferenceMessenger.Default.Register<ErrorWindowCloseMessage>(this, (recipient, msg) => this.Close());
14+
}
15+
}
16+
}

src/WEventViewer/MainWindow.axaml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@
1818
</MenuItem>
1919
<MenuItem Header="_About"/>
2020
</Menu>
21-
<DataGrid
22-
BorderBrush="Black"
23-
Margin="20"
24-
BorderThickness="1"
25-
IsReadOnly="True">
26-
</DataGrid>
21+
<ScrollViewer>
22+
<DataGrid
23+
BorderBrush="Black"
24+
Margin="20"
25+
BorderThickness="1"
26+
IsReadOnly="True" ItemsSource="{Binding LogRecords}" VerticalAlignment="Stretch" MaxHeight="500">
27+
<DataGrid.Columns>
28+
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
29+
<DataGridTextColumn Header="LogName" Binding="{Binding LogName}"/>
30+
<DataGridTextColumn Header="Description" Binding="{Binding Formatted}"/>
31+
</DataGrid.Columns>
32+
<DataGrid.RowHeight>50</DataGrid.RowHeight>
33+
</DataGrid>
34+
</ScrollViewer>
35+
<StackPanel Orientation="Horizontal">
36+
<Label Content="{Binding LogCount}"/>
37+
</StackPanel>
2738
</StackPanel>
2839
</Window>

src/WEventViewer/MainWindow.axaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace WEventViewer;
77

8+
internal record class OpenErrorLogWindow(string message);
89
public partial class MainWindow : Window
910
{
1011
public MainWindow()
@@ -18,11 +19,17 @@ public MainWindow()
1819
{
1920
DataContext = vm
2021
};
21-
var ret = await dlg.ShowDialog<OpenLogWindowViewModel>(this);
22+
var ret = await dlg.ShowDialog<bool>(this);
2223
if(DataContext is MainWindowViewModel mwvm)
2324
{
2425
WeakReferenceMessenger.Default.Send<LoadLogMessage>(new(vm.LogName, vm.PathType));
2526
}
2627
});
28+
WeakReferenceMessenger.Default.Register<MainWindow, OpenErrorLogWindow>(this, async (mw, msg) =>
29+
{
30+
var vm = new ErrorWindowViewModel(msg.message);
31+
var dlg = new ErrorWindow() { DataContext = vm };
32+
await dlg.ShowDialog(mw);
33+
});
2734
}
2835
}

src/WEventViewer/Model/EventLogRepository.cs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,66 @@ internal record class LogRecord(Guid? ActivityId,
3737
string Formatted);
3838
internal static class EventLogRepositoryExtension
3939
{
40+
static readonly DiagnosticListener _DS = new DiagnosticListener(nameof(EventLogRepositoryExtension));
4041
public static LogRecord ToLogRecord(this EventRecord record)
4142
{
42-
return new LogRecord(record.ActivityId, record.Id, record.Keywords, record.KeywordsDisplayNames.ToArray(), record.Level,
43-
record.LevelDisplayName, record.LogName, record.MachineName, record.Opcode, record.OpcodeDisplayName, record.ProcessId,
43+
var keywords = record.Keywords;
44+
var keywordDisplayNames = GetKeywordsDisplayNames(record);
45+
var levelName = GetLevelDisplayName(record);
46+
var taskDisplayName = GetTaskDisplayName(record);
47+
var opcodeDisplayName = GetOpCodeDisplayName(record);
48+
return new LogRecord(record.ActivityId, record.Id, keywords, keywordDisplayNames, record.Level,
49+
levelName, record.LogName, record.MachineName, record.Opcode, opcodeDisplayName, record.ProcessId,
4450
record.Properties.Select(x => x.Value).ToArray(), record.ProviderId, record.ProviderName, record.Qualifiers, record.RecordId, record.RelatedActivityId,
45-
record.Task, record.TaskDisplayName, record.ThreadId, record.TimeCreated, record.Version, record.FormatDescription());
51+
record.Task, taskDisplayName, record.ThreadId, record.TimeCreated, record.Version, record.FormatDescription());
52+
}
53+
static string[] GetKeywordsDisplayNames(EventRecord record)
54+
{
55+
try
56+
{
57+
return record.KeywordsDisplayNames.ToArray();
58+
}
59+
catch (EventLogException ex)
60+
{
61+
_DS.Write("Exception", new { Property = "KeyworkDisplayName", ex, record.LogName, record.ProviderName, record.Id });
62+
return Array.Empty<string>();
63+
}
64+
}
65+
static string GetLevelDisplayName(EventRecord record)
66+
{
67+
try
68+
{
69+
return record.LevelDisplayName;
70+
}
71+
catch (EventLogException ex)
72+
{
73+
_DS.Write("Exception", new { Property = "LevelDisplayName", ex, record.LogName, record.ProviderName, record.Id });
74+
return string.Empty;
75+
}
76+
}
77+
static string GetTaskDisplayName(EventRecord record)
78+
{
79+
try
80+
{
81+
return record.TaskDisplayName;
82+
}
83+
catch (EventLogException ex)
84+
{
85+
_DS.Write("Exception", new { Property = "TaskDisplayName", ex = ex, record.LogName, record.ProviderName, record.Id });
86+
return string.Empty;
87+
}
88+
}
89+
static string GetOpCodeDisplayName(EventRecord record)
90+
{
91+
try
92+
{
93+
return record.OpcodeDisplayName;
94+
}
95+
catch (EventLogException ex)
96+
{
97+
_DS.Write("Exception", new { Property = "OpcodeDisplayName", ex, record.LogName, record.ProviderName, record.Id });
98+
return string.Empty;
99+
}
46100
}
47101
}
48102
internal class EventLogRepository
@@ -80,7 +134,12 @@ public async Task Load(string logName, PathType pathType, string? query, Cancell
80134
_DS.Write("Error", new { Exception = ex, LogRecordDescription = record.FormatDescription() });
81135
throw;
82136
}
137+
if(records.Count >= 1000)
138+
{
139+
break;
140+
}
83141
}
142+
84143
progress?.Report(count);
85144
}
86145
}

src/WEventViewer/OpenLogWindow.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
<TextBox Margin="10,10,10,10" Padding="10,0,10,10" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=PathType, Mode=TwoWay,Converter={StaticResource PathTypeConverter}}"/>
2525
</StackPanel>
2626
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
27-
<Button Content="OK"/>
28-
<Button Content="Cancel"/>
27+
<Button Content="OK" Command="{ Binding OkCommand }"/>
28+
<Button Content="Cancel" Command="{ Binding CancelCommand }"/>
2929
</StackPanel>
3030
</StackPanel>
3131
</Window>

src/WEventViewer/OpenLogWindow.axaml.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55

66
namespace WEventViewer;
77

8+
internal record class OpenDialogResultMessage(bool isOk);
89
public partial class OpenLogWindow : Window
910
{
10-
internal record class OpenDialogResultMessage(bool isOk);
1111
public OpenLogWindow()
1212
{
1313
InitializeComponent();
1414
WeakReferenceMessenger.Default.Register<OpenLogWindow, OpenDialogResultMessage>(this, (recipient, msg) =>
1515
{
16-
if(msg.isOk)
17-
{
18-
19-
}
16+
this.Close(msg.isOk);
2017
});
2118
}
2219
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using CommunityToolkit.Mvvm.Input;
2+
using CommunityToolkit.Mvvm.Messaging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Input;
9+
10+
namespace WEventViewer.ViewModel
11+
{
12+
internal record class ErrorWindowCloseMessage();
13+
internal class ErrorWindowViewModel
14+
{
15+
public ErrorWindowViewModel() : this("")
16+
{
17+
}
18+
public ErrorWindowViewModel(string message)
19+
{
20+
Message = message;
21+
CloseCommand = new RelayCommand(() =>
22+
{
23+
WeakReferenceMessenger.Default.Send<ErrorWindowCloseMessage>(new());
24+
});
25+
}
26+
public string Message { get; set; }
27+
public ICommand CloseCommand { get; set; }
28+
}
29+
}

src/WEventViewer/ViewModel/MainWindowViewModel.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
using System.Collections.ObjectModel;
1616
using System.Collections.Specialized;
1717
using Avalonia;
18+
using System.ComponentModel;
19+
using System.Security.Cryptography.X509Certificates;
20+
1821

1922
namespace WEventViewer.ViewModel
2023
{
@@ -24,12 +27,15 @@ class OpenLogRequest
2427
public PathType PathType { get; set; }
2528
}
2629
record class LoadLogMessage(string logName, PathType pathType);
27-
internal class MainWindowViewModel
30+
internal class MainWindowViewModel : ObservableRecipient
2831
{
29-
EventLogRepository? _EventLogRepository;
32+
EventLogRepository _EventLogRepository;
3033
public MainWindowViewModel()
3134
{
32-
_Progress = new Progress<long>(l => LogCount = l);
35+
_Progress = new Progress<long>(l =>
36+
{
37+
_LogCount = l;
38+
});
3339
_EventLogRepository = new EventLogRepository();
3440
_EventLogRepository.Records.CollectionChanged += Records_CollectionChanged;
3541
OpenCommand = new RelayCommand(() =>
@@ -38,7 +44,14 @@ public MainWindowViewModel()
3844
});
3945
WeakReferenceMessenger.Default.Register<MainWindowViewModel, LoadLogMessage>(this, async (vm, msg) =>
4046
{
41-
await _EventLogRepository.Load(msg.logName, msg.pathType, null, default, _Progress);
47+
try
48+
{
49+
await _EventLogRepository.Load(msg.logName, msg.pathType, null, default, _Progress);
50+
}
51+
catch (Exception ex)
52+
{
53+
WeakReferenceMessenger.Default.Send(new OpenErrorLogWindow(ex.ToString()));
54+
}
4255
});
4356
}
4457

@@ -64,4 +77,9 @@ public long LogCount
6477
}
6578
public ObservableCollection<LogRecord> LogRecords => _EventLogRepository.Records;
6679
}
80+
public class MyRecord(string a, int b)
81+
{
82+
public string A => a;
83+
public int B => b;
84+
}
6785
}

0 commit comments

Comments
 (0)