Skip to content

Commit

Permalink
add help window and commandline options(#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
itn3000 committed Sep 3, 2024
1 parent 3d1880f commit 6a16062
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 22 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ This program is viewer for Windows EventLog
* double click on log, then open detailed log information
* select log and right-click and select "Copy as XML" in context menu, then EventLog XML will be copied to clipboard

## Commandline Options

* `-n/--logname LOGNAME`: target LogName or exported windows eventlog file path
* `-t/--logtype LOGTYPE`: LogName kind, 'logname': from Windows EventLog store, 'filepath': exported Windows Event Log file(*.evtx)
* `-l/--loglevel LOGLEVEL`: LogLevel filter: available values: critical,error,warning,information,verbose
* `-p/--provider PROVIDER`: LogProvider filter
* `-b/--begin BEGINDATE`: createAt filter begin time
* `-e/--end ENDDATE`: createAt filter end time
* `-r/--raw RAW_QUERY`: raw filtering query
* `-h/--help`: display help window

# Screen Shots

MainWindow:
Expand Down
78 changes: 56 additions & 22 deletions src/WEventViewer/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Reflection.Metadata.Ecma335;
using WEventViewer.Model;
using WEventViewer.ViewModel;
Expand All @@ -20,11 +21,12 @@ class WEventViewOptions
public string LogType = "";
public List<int> LogLevels = new List<int>();
}
static bool ShowHelp = false;
static OptionSet CreateOptionSet(OpenLogWindowViewModel vm)
{
var set = new OptionSet()
.Add("n=|logname=", x => vm.LogName = x)
.Add("t=|logtype=", x =>
.Add("n=|logname=", "LogName or Exported EventLog file path(*.evtx)", x => vm.LogName = x)
.Add("t=|logtype=", "LogName kind, 'logname': from Windows EventLog store, 'filepath': exported Windows Event Log file(*.evtx) ", x =>
{
vm.CurrentSelected = x.ToLower() switch
{
Expand All @@ -33,7 +35,7 @@ static OptionSet CreateOptionSet(OpenLogWindowViewModel vm)
_ => throw new ArgumentException("invalid logtype")
};
})
.Add("l=|loglevel=", x =>
.Add("l=|loglevel=", "log severity: availables = critical,error,warning,information,verbose", x =>
{
switch(x.ToLower())
{
Expand All @@ -55,26 +57,27 @@ static OptionSet CreateOptionSet(OpenLogWindowViewModel vm)
}
vm.UseFilterByLevel = true;
})
.Add("p=|provider=", x => vm.ProviderNames = x)
.Add("b=|begin=", x =>
.Add("p=|provider=", "log provider name", x => vm.ProviderNames = x)
.Add("b=|begin=", "log begin datetime", x =>
{
DateTime dt = DateTime.Parse(x);
vm.BeginDate = dt.ToString("yyyy-MM-dd");
vm.BeginTime = dt.ToString("HH:mm:ss");
vm.UseTimeCreated = true;
})
.Add("e=|end=", x =>
.Add("e=|end=", "log end datetime", x =>
{
DateTime dt = DateTime.Parse(x);
vm.EndDate = dt.ToString("yyyy-MM-dd");
vm.EndTime = dt.ToString("HH:mm:ss");
vm.UseTimeCreated = true;
})
.Add("r=|raw=", x =>
.Add("r=|raw=", "log filtering query by raw filter string", x =>
{
vm.RawQuery = x;
vm.UseRawQuery = true;
})
.Add("h|help", x => ShowHelp = true)
;
return set;
}
Expand All @@ -89,19 +92,24 @@ public override void OnFrameworkInitializationCompleted()
collection.AddSingleton<EventLogRepository>();
collection.AddSingleton<IViewModelFactory, ViewModelFactoryServiceProvider>(provider => new ViewModelFactoryServiceProvider(provider));
collection.AddSingleton<MainWindowViewModel>();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime classic)
{
collection.AddSingleton<OpenLogWindowViewModel>(provider =>
{
var vm = new OpenLogWindowViewModel();
if (classic.Args != null)
{
var optset = CreateOptionSet(vm);
optset.Parse(classic.Args);
}
return vm;
});
}
collection.AddSingleton<OpenLogWindowViewModel>();
//if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime classic)
//{
// collection.AddSingleton<OpenLogWindowViewModel>(provider =>
// {
// var vm = new OpenLogWindowViewModel();
// if (classic.Args != null)
// {
// var optset = CreateOptionSet(vm);
// var remaining = optset.Parse(classic.Args);
// if(remaining.Count > 0)
// {
// vm.LogName = remaining[0];
// }
// }
// return vm;
// });
//}
collection.AddSingleton<MainWindow>(provider =>
{
return new MainWindow(provider.GetRequiredService<IViewModelFactory>())
Expand All @@ -116,11 +124,37 @@ public override void OnFrameworkInitializationCompleted()
collection.AddTransient<LogNameViewModel>();
collection.AddTransient<AboutViewModel>();
collection.AddTransient<DetailedLogViewModel>();
collection.AddTransient<HelpWindowViewModel>(provider =>
{
var optset = CreateOptionSet(provider.GetRequiredService<OpenLogWindowViewModel>());
using var sw = new StringWriter();
optset.WriteOptionDescriptions(sw);
return new HelpWindowViewModel() { HelpMessage = sw.ToString() };
});
collection.AddTransient<HelpWindow>(provider => new HelpWindow() { DataContext = provider.GetRequiredService<HelpWindowViewModel>() });
var serviceProvider = collection.BuildServiceProvider();
var vm = serviceProvider.GetRequiredService<MainWindowViewModel>();
//var vm = serviceProvider.GetRequiredService<MainWindowViewModel>();
var w = serviceProvider.GetRequiredService<MainWindow>();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = serviceProvider.GetRequiredService<MainWindow>();
var openLogVM = serviceProvider.GetRequiredService<OpenLogWindowViewModel>();
var optset = CreateOptionSet(openLogVM);
if (desktop.Args != null)
{
var remaining = optset.Parse(desktop.Args);
if(remaining.Count > 0)
{
openLogVM.LogName = remaining[0];
}
}
if (ShowHelp)
{
desktop.MainWindow = serviceProvider.GetRequiredService<HelpWindow>();
}
else
{
desktop.MainWindow = w;
}
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
Expand Down
26 changes: 26 additions & 0 deletions src/WEventViewer/HelpWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:WEventViewer.ViewModel"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="450"
x:Class="WEventViewer.HelpWindow"
x:DataType="vm:HelpWindowViewModel"
Title="HelpWindow">
<Design.DataContext>
<vm:HelpWindowViewModel/>
</Design.DataContext>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" MaxWidth="600" HorizontalScrollBarVisibility="Auto">
<TextBlock Text="{Binding HelpMessage}" HorizontalAlignment="Left" MaxWidth="{Binding $parent.Bounds.Width}"/>
</ScrollViewer>
<Button Name="CloseButton" Content="Close" HorizontalAlignment="Right" Grid.Row="1" Click="Button_Click"/>
</Grid>
</Window>
18 changes: 18 additions & 0 deletions src/WEventViewer/HelpWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace WEventViewer;

public partial class HelpWindow : Window
{
public HelpWindow()
{
InitializeComponent();
}

private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
Close();
}
}
11 changes: 11 additions & 0 deletions src/WEventViewer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"WEventViewer": {
"commandName": "Project"
},
"WEventViewer-Help": {
"commandName": "Project",
"commandLineArgs": "--help"
}
}
}
13 changes: 13 additions & 0 deletions src/WEventViewer/ViewModel/HelpWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WEventViewer.ViewModel
{
public class HelpWindowViewModel
{
public string HelpMessage { get; set; } = "";
}
}

0 comments on commit 6a16062

Please sign in to comment.