-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
62 lines (51 loc) · 1.93 KB
/
Copy pathApp.xaml.cs
File metadata and controls
62 lines (51 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.IO;
using System.Windows;
using System.Windows.Threading;
using HandshakeDVPN.Services;
namespace HandshakeDVPN;
public partial class App : Application
{
public static IHnsVpnBackend Backend { get; private set; } = null!;
private static readonly string CrashLog = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"HandshakeDVPN", "crash.log");
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Catch all unhandled exceptions
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnDomainUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
Backend = new NativeVpnClient(null);
}
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogCrash("Dispatcher", e.Exception);
e.Handled = true; // prevent crash, keep app running
}
private static void OnDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex) LogCrash("Domain", ex);
}
private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
LogCrash("Task", e.Exception);
e.SetObserved(); // prevent crash
}
private static void LogCrash(string source, Exception ex)
{
try
{
var dir = Path.GetDirectoryName(CrashLog)!;
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var msg = $"[{DateTime.Now:HH:mm:ss}] [{source}] {ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}\n\n";
File.AppendAllText(CrashLog, msg);
}
catch { }
}
protected override void OnExit(ExitEventArgs e)
{
Backend?.Dispose();
base.OnExit(e);
}
}