-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
162 lines (141 loc) · 4.86 KB
/
Copy pathProgram.cs
File metadata and controls
162 lines (141 loc) · 4.86 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Layout;
using Avalonia.Native;
using Avalonia.Media;
using Avalonia.Threading;
namespace RawImporterCS;
internal static class Program
{
[STAThread]
public static void Main(string[] args)
{
RegisterGlobalExceptionHandlers();
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
public static AppBuilder BuildAvaloniaApp()
{
var builder = AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
if (OperatingSystem.IsMacOS())
{
builder = builder.With(new AvaloniaNativePlatformOptions
{
RenderingMode = new[] { AvaloniaNativeRenderingMode.Software }
});
}
return builder;
}
private static void RegisterGlobalExceptionHandlers()
{
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
if (args.ExceptionObject is Exception ex)
{
var logPath = LogCrash("AppDomain.UnhandledException", ex);
ShowCrashNotice(logPath, ex);
}
};
TaskScheduler.UnobservedTaskException += (_, args) =>
{
var logPath = LogCrash("TaskScheduler.UnobservedTaskException", args.Exception);
args.SetObserved();
ShowCrashNotice(logPath, args.Exception);
};
}
private static string LogCrash(string source, Exception exception)
{
var logPath = GetCrashLogPath();
try
{
var directory = Path.GetDirectoryName(logPath);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
using var writer = new StreamWriter(logPath, append: true);
writer.WriteLine($"[{DateTimeOffset.Now:u}] {source}");
WriteException(writer, exception, 0);
writer.WriteLine(new string('-', 60));
}
catch
{
// ignore logging errors to keep crash path consistent
}
return logPath;
}
private static void WriteException(TextWriter writer, Exception exception, int depth)
{
var indent = new string(' ', depth * 2);
writer.WriteLine($"{indent}{exception.GetType().FullName}: {exception.Message}");
if (!string.IsNullOrWhiteSpace(exception.StackTrace))
{
writer.WriteLine($"{indent}{exception.StackTrace}");
}
if (exception is AggregateException aggregate)
{
foreach (var inner in aggregate.InnerExceptions)
{
WriteException(writer, inner, depth + 1);
}
}
if (exception.InnerException != null)
{
WriteException(writer, exception.InnerException, depth + 1);
}
}
private static string GetCrashLogPath()
{
var baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (string.IsNullOrWhiteSpace(baseDirectory))
{
baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
if (string.IsNullOrWhiteSpace(baseDirectory))
{
baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".rawimportercs");
}
var directory = Path.Combine(baseDirectory, "RawImporterCS");
return Path.Combine(directory, "crash.log");
}
private static void ShowCrashNotice(string logPath, Exception exception)
{
try
{
Console.Error.WriteLine($"Fatal error: {exception}");
Console.Error.WriteLine($"Crash log: {logPath}");
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: not null })
{
Dispatcher.UIThread.Post(() =>
{
var notice = new Window
{
Title = "RAW Importer abgestürzt",
Width = 420,
Height = 200,
CanResize = false,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Content = new TextBlock
{
Margin = new Thickness(16),
TextWrapping = TextWrapping.Wrap,
Text = $"App ist abgestürzt. Siehe crash.log:\n{logPath}"
}
};
notice.Show();
});
}
}
catch
{
// Keep shutdown path resilient
}
}
}