-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (65 loc) · 2.17 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (65 loc) · 2.17 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
63
64
65
66
67
68
69
70
71
72
73
74
using Avalonia;
using Avalonia.Win32;
using System;
using System.IO;
using System.Threading.Tasks;
namespace OpenSysKit.UI;
internal sealed class Program
{
private static readonly string CrashLogDir =
Path.Combine(AppContext.BaseDirectory, "crash-logs");
[STAThread]
public static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
try
{
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
WriteCrashLog("Main_Catch", ex);
throw;
}
}
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.With(new Win32PlatformOptions
{
RenderingMode = [Win32RenderingMode.AngleEgl, Win32RenderingMode.Software],
CompositionMode = [Win32CompositionMode.WinUIComposition, Win32CompositionMode.RedirectionSurface],
})
.LogToTrace();
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
WriteCrashLog("UnhandledException", e.ExceptionObject as Exception);
}
private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
WriteCrashLog("UnobservedTaskException", e.Exception);
e.SetObserved();
}
private static void WriteCrashLog(string source, Exception? ex)
{
try
{
Directory.CreateDirectory(CrashLogDir);
var fileName = $"crash_{DateTime.Now:yyyyMMdd_HHmmss}_{source}.log";
var path = Path.Combine(CrashLogDir, fileName);
var content = $"""
=== OpenSysKit Crash Report ===
Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}
Source: {source}
{ex}
""";
File.WriteAllText(path, content);
}
catch
{
// last resort: nothing we can do
}
}
}