Skip to content

Commit 402cd4b

Browse files
author
David Khristepher Santos
committed
Add basic logging
1 parent b86cdbb commit 402cd4b

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

PSXPackagerGUI/App.xaml.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Linq;
66
using System.Threading.Tasks;
77
using System.Windows;
8+
using System.Windows.Threading;
9+
using PSXPackagerGUI.Common;
810

911
namespace PSXPackagerGUI
1012
{
@@ -13,5 +15,21 @@ namespace PSXPackagerGUI
1315
/// </summary>
1416
public partial class App : Application
1517
{
18+
public App()
19+
{
20+
this.DispatcherUnhandledException += OnDispatcherUnhandledException;
21+
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
22+
}
23+
24+
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
25+
{
26+
Logger.LogError($"An unhandled exception occured.", e.ExceptionObject as Exception);
27+
}
28+
29+
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
30+
{
31+
Logger.LogError("An unhandled exception occured.", e.Exception);
32+
e.Handled = true;
33+
}
1634
}
1735
}

PSXPackagerGUI/Common/Logger.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace PSXPackagerGUI.Common
5+
{
6+
public static class Logger
7+
{
8+
private static string LogPath = Path.Combine(ApplicationInfo.AppPath, "logs.txt");
9+
10+
public static void LogError(string message, Exception exception)
11+
{
12+
File.AppendAllText(LogPath, $"{message}. {exception}\r\n");
13+
}
14+
15+
public static void LogInfo(string message)
16+
{
17+
File.AppendAllText(LogPath, $"{message}\r\n");
18+
}
19+
}
20+
}

PSXPackagerGUI/MainWindow.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ public partial class MainWindow : Window
2424

2525
public MainWindow()
2626
{
27+
28+
Logger.LogInfo("Starting up");
29+
2730
InitializeComponent();
2831

32+
Logger.LogInfo("Creating Settings Page");
2933
_settings = new SettingsPage(this);
3034
_isFirstRun = _settings.IsFirstRun;
3135

3236

37+
Logger.LogInfo("Creating Single Page");
3338
_singlePage = new SinglePage(this, _settings.Model, _gameDb);
39+
40+
Logger.LogInfo("Creating Batch Page");
3441
_batchPage = new BatchPage(this, _settings.Model, _gameDb);
3542
_singlePage.Model.PropertyChanged += ModelOnPropertyChanged;
3643

44+
Logger.LogInfo("Creating Main Model");
3745
_model = new MainModel();
3846

3947
DataContext = _model;

0 commit comments

Comments
 (0)