Skip to content

Commit b91497b

Browse files
Add logs when the settings app crashes
GH-104
1 parent edb6898 commit b91497b

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

src/KeyboardSwitch.Core/Services/Settings/JsonSettingsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public async Task<AppSettings> GetAppSettings(bool strict = false)
7575

7676
public async Task SaveAppSettings(AppSettings appSettings)
7777
{
78-
logger.LogDebug("Saving the app settings");
78+
logger.LogInformation("Saving the app settings");
7979

8080
this.file.Directory?.Create();
8181
this.file.Truncate();
@@ -88,7 +88,7 @@ public async Task SaveAppSettings(AppSettings appSettings)
8888

8989
public void InvalidateAppSettings()
9090
{
91-
logger.LogDebug("Invalidating the app settings");
91+
logger.LogInformation("Invalidating the app settings");
9292

9393
this.appSettings = null;
9494
this.settingsInvalidated.OnNext(Unit.Default);

src/KeyboardSwitch.Settings.Core/ViewModels/ReactiveForm.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@ protected void TrackValidationStrict(IObservable<bool> validation) =>
7070
this.validationsToTrack.Add(validation.StartWith(false));
7171

7272
protected IObservable<bool> IsCollectionChanged<TOtherForm, TOtherModel>(
73-
Expression<Func<TForm, ReadOnlyObservableCollection<TOtherForm>>> property,
73+
Func<TForm, ReadOnlyObservableCollection<TOtherForm>> property,
7474
Func<TForm, ICollection<TOtherModel>> itemCollection)
7575
where TOtherForm : ReactiveForm<TOtherModel, TOtherForm>
76-
where TOtherModel : class
77-
{
78-
string propertyName = property.GetMemberName();
79-
80-
return property.Compile()(this.Self)
76+
where TOtherModel : class =>
77+
property(this.Self)
8178
.ToObservableChangeSet()
8279
.AutoRefreshOnObservable(vm => vm.FormChanged)
8380
.ToCollection()
@@ -86,22 +83,17 @@ protected IObservable<bool> IsCollectionChanged<TOtherForm, TOtherModel>(
8683
vms.Any(vm => vm.IsFormChanged || !this.IsNew && vm.IsNew))
8784
.Merge(this.Save.Select(_ => false))
8885
.Merge(this.Cancel.Select(_ => false));
89-
}
9086

9187
protected IObservable<bool> IsCollectionChangedSimple<TItem>(
92-
Expression<Func<TForm, ReadOnlyObservableCollection<TItem>>> property,
88+
Func<TForm, ReadOnlyObservableCollection<TItem>> property,
9389
Func<TForm, ICollection<TItem>> itemCollection)
94-
where TItem : notnull
95-
{
96-
string propertyName = property.GetMemberName();
97-
98-
return property.Compile()(this.Self)
90+
where TItem : notnull =>
91+
property(this.Self)
9992
.ToObservableChangeSet()
10093
.ToCollection()
10194
.Select(items => !Enumerable.SequenceEqual(items, itemCollection(this.Self)))
10295
.Merge(this.Save.Select(_ => false))
10396
.Merge(this.Cancel.Select(_ => false));
104-
}
10597

10698
protected IObservable<bool> IsCollectionValid<TOtherForm>(ReadOnlyObservableCollection<TOtherForm> viewModels)
10799
where TOtherForm : IReactiveForm =>
@@ -115,7 +107,7 @@ protected ValidationHelper LocalizedValidationRule<T>(
115107
Expression<Func<TForm, T?>> property,
116108
Func<T?, bool> validate)
117109
{
118-
var propertyName = property.GetMemberName();
110+
string propertyName = property.GetMemberName();
119111
return this.Self.ValidationRule(
120112
property, validate, _ => this.ResourceManager.GetString($"{propertyName}Invalid") ?? String.Empty);
121113
}

src/KeyboardSwitch.Settings/App.axaml.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ private async Task<MainViewModel> InitializeApp()
6666
{
6767
TransitioningContentControl.PageTransitionProperty.OverrideDefaultValue(typeof(ViewModelViewHost), null);
6868

69+
RxApp.DefaultExceptionHandler = Observer.Create<Exception>(e =>
70+
this.Log().Error(e, "Unhandled exception in the settings app"));
71+
6972
var services = new ServiceCollection();
7073
this.ConfigureServices(services);
7174

@@ -83,8 +86,6 @@ private async Task<MainViewModel> InitializeApp()
8386
{
8487
var appSettings = await this.serviceProvider.GetRequiredService<IAppSettingsService>().GetAppSettings();
8588

86-
this.SetTheme(appSettings.AppTheme);
87-
8889
var mainViewModel = new MainViewModel(appSettings);
8990
openExternally.InvokeCommand(mainViewModel.OpenExternally);
9091

@@ -111,7 +112,7 @@ private async Task<MainViewModel> InitializeApp()
111112
"Delete the settings and let the app recreate a compatible version",
112113
e.Version);
113114

114-
this.desktop.Shutdown(2);
115+
this.desktop.Shutdown((int)ExitCode.IncompatibleSettingsVersion);
115116
return null!;
116117
}
117118
}
@@ -125,6 +126,7 @@ private void ConfigureServices(IServiceCollection services)
125126
var config = new ConfigurationRoot([genericProvider, platformSpecificProvider]);
126127

127128
var logger = SerilogLoggerFactory.CreateLogger(config);
129+
Log.Logger = logger;
128130

129131
services
130132
.AddOptions()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace KeyboardSwitch.Settings;
2+
3+
public enum ExitCode
4+
{
5+
Success = 0,
6+
Error = 1,
7+
IncompatibleSettingsVersion = 2
8+
}

src/KeyboardSwitch.Settings/Program.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1+
using Serilog;
2+
3+
using Constants = Serilog.Core.Constants;
4+
15
namespace KeyboardSwitch.Settings;
26

37
public static class Program
48
{
59
[STAThread]
6-
public static void Main(string[] args)
10+
public static int Main(string[] args)
711
{
8-
Directory.SetCurrentDirectory(Path.GetDirectoryName(AppContext.BaseDirectory) ?? String.Empty);
12+
try
13+
{
14+
Directory.SetCurrentDirectory(Path.GetDirectoryName(AppContext.BaseDirectory) ?? String.Empty);
15+
16+
return BuildAvaloniaApp()
17+
.StartWithClassicDesktopLifetime(args);
18+
} catch (Exception e)
19+
{
20+
Log.ForContext(Constants.SourceContextPropertyName, typeof(Program).FullName)
21+
.Fatal(e, "The settings app has crashed");
922

10-
BuildAvaloniaApp()
11-
.StartWithClassicDesktopLifetime(args);
23+
return (int)ExitCode.Error;
24+
} finally
25+
{
26+
Log.CloseAndFlush();
27+
}
1228
}
1329

1430
public static AppBuilder BuildAvaloniaApp() =>

src/KeyboardSwitch.Windows/KeyboardSwitch.Windows.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<PackageReference Include="System.Interactive" Version="6.0.3" />
1212
<PackageReference Include="System.Interactive.Async" Version="6.0.3" />
1313
<PackageReference Include="System.Reactive" Version="6.0.1" />
14-
<PackageReference Include="Vanara.Core" Version="4.1.4" />
15-
<PackageReference Include="Vanara.PInvoke.Kernel32" Version="4.1.4" />
16-
<PackageReference Include="Vanara.PInvoke.Shared" Version="4.1.4" />
17-
<PackageReference Include="Vanara.PInvoke.User32" Version="4.1.4" />
14+
<PackageReference Include="Vanara.Core" Version="4.1.6" />
15+
<PackageReference Include="Vanara.PInvoke.Kernel32" Version="4.1.6" />
16+
<PackageReference Include="Vanara.PInvoke.Shared" Version="4.1.6" />
17+
<PackageReference Include="Vanara.PInvoke.User32" Version="4.1.6" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)