Skip to content

Portable Tool for an Ideal Windows Setup

-added Error Logging

Greedeks7c4485e859b2
diff --git a/.Source/GTweak/App.xaml.cs b/.Source/GTweak/App.xaml.cs
index 09df2d2c..845fd54c 100644
--- a/.Source/GTweak/App.xaml.cs
+++ b/.Source/GTweak/App.xaml.cs
@@ -1,9 +1,11 @@
-using GTweak.Utilities.Helpers;
+using GTweak.Utilities.Control;
+using GTweak.Utilities.Helpers;
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
+using System.Windows.Threading;
namespace GTweak
{
@@ -18,6 +20,23 @@ namespace GTweak
public App()
{
InitializeComponent();
+
+ this.DispatcherUnhandledException += OnDispatcherUnhandledException;
+ AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
+ }
+
+ private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
+ {
+ ErrorLogging.LogWritingFile(e.Exception);
+ e.Handled = true;
+ Environment.Exit(0);
+ }
+
+ private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
+ {
+ if (e.ExceptionObject is Exception ex)
+ ErrorLogging.LogWritingFile(ex);
+ Environment.Exit(0);
}
internal static void UpdateImport() => ImportTweaksUpdate?.Invoke(default, EventArgs.Empty);
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index 4d434b1e..0698d4a8 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -265,6 +265,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Utilities\Control\BlockRunTweaker.cs" />
+ <Compile Include="Utilities\Control\ErrorLogging.cs" />
<Compile Include="Utilities\Helpers\DisablingWinKeys.cs" />
<Compile Include="Utilities\Helpers\CommandExecutor.cs" />
<Compile Include="Utilities\Helpers\ExplorerManager.cs" />
diff --git a/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs b/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
index cfffd698..80cadd4a 100644
--- a/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
+++ b/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
@@ -1,4 +1,5 @@
-using System;
+using GTweak.Utilities.Control;
+using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
@@ -92,7 +93,7 @@ namespace GTweak.Utilities.Configuration
}
catch (Exception ex)
{
- Debug.WriteLine(ex.Message);
+ ErrorLogging.LogDebug(ex);
return 0;
}
}
diff --git a/.Source/GTweak/Utilities/Control/ErrorLogging.cs b/.Source/GTweak/Utilities/Control/ErrorLogging.cs
new file mode 100644
index 00000000..6e7b84ad
--- /dev/null
+++ b/.Source/GTweak/Utilities/Control/ErrorLogging.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace GTweak.Utilities.Control
+{
+ internal class ErrorLogging
+ {
+ private static readonly string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GTweak_Error.log");
+
+ internal static void LogWritingFile(Exception ex) => Task.Run(() => LogToFile(ex));
+
+ internal static void LogDebug(Exception ex) => Debug.WriteLine($"Debug: {ex.Message}\nStack Trace: {ex.StackTrace}");
+
+ internal static async Task LogToFile(Exception ex)
+ {
+ try
+ {
+ using var stream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
+ using var writer = new StreamWriter(stream, new UTF8Encoding(false));
+ await writer.WriteLineAsync($"[{DateTime.Now}] Error: {ex.Message}\nStack Trace:\n{ex.StackTrace}\n");
+ }
+ catch (Exception fileEx)
+ {
+ LogDebug(fileEx);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/.Source/GTweak/Utilities/Control/SettingsRepository.cs b/.Source/GTweak/Utilities/Control/SettingsRepository.cs
index 761c023e..5200bd8c 100644
--- a/.Source/GTweak/Utilities/Control/SettingsRepository.cs
+++ b/.Source/GTweak/Utilities/Control/SettingsRepository.cs
@@ -3,7 +3,6 @@ using GTweak.Windows;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
-using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -130,7 +129,7 @@ namespace GTweak.Utilities.Control
iniManager.WriteAll(INIManager.SectionSvc, INIManager.TempTweaksSvc);
iniManager.WriteAll(INIManager.SectionSys, INIManager.TempTweaksSys);
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
@@ -170,7 +169,7 @@ namespace GTweak.Utilities.Control
CommandExecutor.RunCommand($"/c taskkill /f /im {currentName} & choice /c y /n /d y /t 3 & del {new FileInfo(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath).Name} & " +
@$"rd /s /q {StoragePaths.FolderLocation} & rd /s /q {Environment.SystemDirectory}\config\systemprofile\AppData\Local\GTweak");
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
internal static void SelfReboot() => CommandExecutor.RunCommand($"/c taskkill /f /im \"{currentName}\" & choice /c y /n /d y /t 1 & start \"\" \"{currentLocation}\"");
diff --git a/.Source/GTweak/Utilities/Helpers/ExplorerManager.cs b/.Source/GTweak/Utilities/Helpers/ExplorerManager.cs
index 463bec77..ca6acb93 100644
--- a/.Source/GTweak/Utilities/Helpers/ExplorerManager.cs
+++ b/.Source/GTweak/Utilities/Helpers/ExplorerManager.cs
@@ -1,4 +1,5 @@
-using System;
+using GTweak.Utilities.Control;
+using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@@ -23,7 +24,7 @@ namespace GTweak.Utilities.Helpers
process.Start();
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
finally
{
if (Process.GetProcessesByName("explorer").Length == 0)
diff --git a/.Source/GTweak/Utilities/Helpers/RegistryHelp.cs b/.Source/GTweak/Utilities/Helpers/RegistryHelp.cs
index 177e5ce8..52b51cfc 100644
--- a/.Source/GTweak/Utilities/Helpers/RegistryHelp.cs
+++ b/.Source/GTweak/Utilities/Helpers/RegistryHelp.cs
@@ -1,6 +1,6 @@
-using Microsoft.Win32;
+using GTweak.Utilities.Control;
+using Microsoft.Win32;
using System;
-using System.Diagnostics;
using System.Threading.Tasks;
namespace GTweak.Utilities.Helpers
@@ -34,7 +34,7 @@ namespace GTweak.Utilities.Helpers
registrykey.OpenSubKey(subkey, true)?.DeleteValue(value);
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}).Wait();
}
@@ -54,7 +54,7 @@ namespace GTweak.Utilities.Helpers
}
catch (Exception ex)
{
- Debug.WriteLine(ex.Message);
+ ErrorLogging.LogDebug(ex);
}
}).Wait();
}
@@ -67,7 +67,7 @@ namespace GTweak.Utilities.Helpers
{
registrykey.CreateSubKey(subkey);
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}).Wait();
}
@@ -90,12 +90,12 @@ namespace GTweak.Utilities.Helpers
foreach (string value in registryFolder.GetValueNames())
{
try { registryFolder.DeleteValue(value); }
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
registrykey.DeleteSubKeyTree(subkey, false);
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}).Wait();
}
diff --git a/.Source/GTweak/Utilities/Tweaks/ClearingMemory.cs b/.Source/GTweak/Utilities/Tweaks/ClearingMemory.cs
index f9d57f01..cbfec96e 100644
--- a/.Source/GTweak/Utilities/Tweaks/ClearingMemory.cs
+++ b/.Source/GTweak/Utilities/Tweaks/ClearingMemory.cs
@@ -152,7 +152,7 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
private static bool SetIncreasePrivilege(string privilegeName)
diff --git a/.Source/GTweak/Utilities/Tweaks/ConfidentialityTweaks.cs b/.Source/GTweak/Utilities/Tweaks/ConfidentialityTweaks.cs
index f23924d2..db7895cf 100644
--- a/.Source/GTweak/Utilities/Tweaks/ConfidentialityTweaks.cs
+++ b/.Source/GTweak/Utilities/Tweaks/ConfidentialityTweaks.cs
@@ -5,7 +5,6 @@ using GTweak.Utilities.Helpers;
using GTweak.View;
using Microsoft.Win32;
using System;
-using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -217,7 +216,7 @@ namespace GTweak.Utilities.Tweaks
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
BlockSpyDomain(isChoose);
diff --git a/.Source/GTweak/Utilities/Tweaks/DefenderManager/BackupRights.cs b/.Source/GTweak/Utilities/Tweaks/DefenderManager/BackupRights.cs
index bb450933..2f42143e 100644
--- a/.Source/GTweak/Utilities/Tweaks/DefenderManager/BackupRights.cs
+++ b/.Source/GTweak/Utilities/Tweaks/DefenderManager/BackupRights.cs
@@ -4,7 +4,6 @@ using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
-using System.Diagnostics;
using System.IO;
using System.Security.AccessControl;
@@ -76,7 +75,7 @@ namespace GTweak.Utilities.Tweaks.DefenderManager
File.WriteAllText(jsonFilePath, JsonConvert.SerializeObject(allValues, Formatting.Indented));
File.WriteAllText(aclFilePath, JsonConvert.SerializeObject(aclDataDict, Formatting.Indented));
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
@@ -114,7 +113,7 @@ namespace GTweak.Utilities.Tweaks.DefenderManager
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
}
diff --git a/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs b/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs
index 84e26d0d..b6530452 100644
--- a/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs
+++ b/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs
@@ -391,7 +391,7 @@ namespace GTweak.Utilities.Tweaks.DefenderManager
foreach (Process process in Process.GetProcessesByName(getName))
{
try { process.Kill(); await Task.Delay(1000); }
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
}
@@ -421,7 +421,7 @@ namespace GTweak.Utilities.Tweaks.DefenderManager
comandoAEjecutar.StandardOutput.ReadToEnd();
return;
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
}
diff --git a/.Source/GTweak/Utilities/Tweaks/InterfaceTweaks.cs b/.Source/GTweak/Utilities/Tweaks/InterfaceTweaks.cs
index 29cc653f..743ab478 100644
--- a/.Source/GTweak/Utilities/Tweaks/InterfaceTweaks.cs
+++ b/.Source/GTweak/Utilities/Tweaks/InterfaceTweaks.cs
@@ -1,9 +1,9 @@
using GTweak.Utilities.Configuration;
+using GTweak.Utilities.Control;
using GTweak.Utilities.Helpers;
using GTweak.View;
using Microsoft.Win32;
using System;
-using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
@@ -227,7 +227,7 @@ namespace GTweak.Utilities.Tweaks
}
});
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
break;
case "TglButton12":
if (isChoose)
diff --git a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs
index 26d52c59..478976a4 100644
--- a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs
+++ b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs
@@ -5,7 +5,6 @@ using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
-using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
@@ -276,11 +275,11 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
break;
case "TglButton9":
RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\WiaRpc", "Start", isChoose ? 4 : 3, RegistryValueKind.DWord);
@@ -475,7 +474,7 @@ namespace GTweak.Utilities.Tweaks
else
SetTaskStateOwner(true, winUpdatesTasks);
}
- catch (Exception ex) { Debug.WriteLine(ex); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
});
}
}
diff --git a/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs b/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
index b7845832..7eaf0b0b 100644
--- a/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
+++ b/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
@@ -85,7 +85,7 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
if (!isTweakWorkingAntivirus)
systemV.TglButton8.StateNA = File.Exists(Path.Combine(StoragePaths.SystemDisk, @"Windows\System32\smartscreen.exe"));
@@ -252,7 +252,7 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
break;
case "TglButton8":
new WindowsDefender().SetState(isChoose);
@@ -495,7 +495,7 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
});
}
}
diff --git a/.Source/GTweak/Utilities/Tweaks/UninstallingPakages.cs b/.Source/GTweak/Utilities/Tweaks/UninstallingPakages.cs
index 8173446b..75e710a7 100644
--- a/.Source/GTweak/Utilities/Tweaks/UninstallingPakages.cs
+++ b/.Source/GTweak/Utilities/Tweaks/UninstallingPakages.cs
@@ -122,7 +122,7 @@ namespace GTweak.Utilities.Tweaks
if (!string.IsNullOrEmpty(Alias))
await RunPowerShell($"{argument} \"Get-AppxProvisionedPackage -online | where-object {{$_.PackageName -like '*{Alias}*'}} | Remove-AppxProvisionedPackage -alluser -online –Verbose\"");
}
- catch (Exception ex) { Debug.WriteLine(ex); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
switch (packageName)
{
@@ -177,12 +177,12 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
baseKey.Close();
}
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
break;
case "Edge":
string script = $@"
@@ -236,7 +236,7 @@ namespace GTweak.Utilities.Tweaks
})?.WaitForExitAsync();
}
- catch (Exception ex) { Debug.WriteLine(ex); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
DeletingTask(edgeTasks);
@@ -303,7 +303,7 @@ namespace GTweak.Utilities.Tweaks
}
}
}
- catch (Exception ex) { Debug.WriteLine(ex); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
break;
}
});
diff --git a/.Source/GTweak/View/MoreView.xaml.cs b/.Source/GTweak/View/MoreView.xaml.cs
index 8fff614e..9d52676b 100644
--- a/.Source/GTweak/View/MoreView.xaml.cs
+++ b/.Source/GTweak/View/MoreView.xaml.cs
@@ -4,7 +4,6 @@ using GTweak.Utilities.Helpers;
using GTweak.Utilities.Tweaks;
using GTweak.Windows;
using System;
-using System.Diagnostics;
using System.Windows.Controls;
namespace GTweak.View
@@ -50,7 +49,7 @@ namespace GTweak.View
BackgroundQueue backgroundQueue = new BackgroundQueue();
await backgroundQueue.QueueTask(delegate
{
- try { SystemMaintenance.DisableRestorePoint(); } catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ try { SystemMaintenance.DisableRestorePoint(); } catch (Exception ex) { ErrorLogging.LogDebug(ex); }
});
await backgroundQueue.QueueTask(delegate { new ViewNotification(300).Show("", "info", "disable_recovery_notification"); });
}
diff --git a/.Source/GTweak/View/PakagesView.xaml.cs b/.Source/GTweak/View/PakagesView.xaml.cs
index ce64b825..769eab72 100644
--- a/.Source/GTweak/View/PakagesView.xaml.cs
+++ b/.Source/GTweak/View/PakagesView.xaml.cs
@@ -118,7 +118,7 @@ namespace GTweak.View
ExplorerManager.Restart(new Process());
});
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
});
break;
}
@@ -148,7 +148,7 @@ namespace GTweak.View
UpdateViewStatePakages();
});
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
});
break;
}
diff --git a/.Source/GTweak/Windows/ImportWindow.xaml.cs b/.Source/GTweak/Windows/ImportWindow.xaml.cs
index 400b1e98..a28d6047 100644
--- a/.Source/GTweak/Windows/ImportWindow.xaml.cs
+++ b/.Source/GTweak/Windows/ImportWindow.xaml.cs
@@ -34,7 +34,7 @@ namespace GTweak.Windows
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
Progress<byte> progress = new Progress<byte>(ReportProgress);
- try { await SortByPageDate(_cancellationTokenSource.Token, progress); } catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ try { await SortByPageDate(_cancellationTokenSource.Token, progress); } catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
private void ReportProgress(byte valueProgress)
diff --git a/.Source/GTweak/Windows/MessageWindow.xaml.cs b/.Source/GTweak/Windows/MessageWindow.xaml.cs
index e984c2d0..f9f97c4f 100644
--- a/.Source/GTweak/Windows/MessageWindow.xaml.cs
+++ b/.Source/GTweak/Windows/MessageWindow.xaml.cs
@@ -1,6 +1,6 @@
-using System;
+using GTweak.Utilities.Control;
+using System;
using System.ComponentModel;
-using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
@@ -28,7 +28,7 @@ namespace GTweak.Windows
}, Application.Current.Dispatcher);
}
- catch (Exception ex) { Debug.WriteLine(ex.Message); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
timer?.Start();
if (isViolationSystem)