Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (77 loc) · 2.98 KB
/
Copy pathProgram.cs
File metadata and controls
86 lines (77 loc) · 2.98 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
using System;
using System.IO;
using System.Threading.Tasks;
namespace PaintTrek
{
public static class Program
{
[STAThread]
static void Main()
{
// Clear previous session logs to keep file size manageable
Logger.Clear();
// Global exception handlers for non-UI threads and tasks
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
LogCrash((Exception)args.ExceptionObject, "AppDomain.UnhandledException");
};
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
LogCrash(args.Exception, "TaskScheduler.UnobservedTaskException");
args.SetObserved();
};
// Global Exception Handler Wrapper
try
{
using var game = new PaintTrek.Game1();
game.Run();
}
catch (Exception ex)
{
LogCrash(ex, "Main Loop Exception");
throw;
}
}
private static void LogCrash(Exception ex, string source)
{
try
{
string crashInfo = $"--------------------------------------------------\n" +
$"Crash Date: {DateTime.Now}\n" +
$"Source: {source}\n" +
$"Exception: {ex.GetType().Name}\n" +
$"Message: {ex.Message}\n" +
$"Source: {ex.Source}\n" +
$"Stack Trace: {ex.StackTrace}\n";
if (ex.InnerException != null)
{
crashInfo += $"Inner Exception: {ex.InnerException.Message}\n" +
$"Inner Stack Trace: {ex.InnerException.StackTrace}\n";
}
crashInfo += "--------------------------------------------------\n";
// Try 1: MyDocuments
try
{
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string logPath = Path.Combine(docPath, "PaintTrek_ErrorLog.txt");
File.AppendAllText(logPath, crashInfo);
}
catch { }
// Try 2: LocalApplicationData (Store Safe)
try
{
string localFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PaintTrek");
Directory.CreateDirectory(localFolder);
string logPath = Path.Combine(localFolder, "crash_store.log");
File.AppendAllText(logPath, crashInfo);
}
catch {
}
}
catch
{
// Last resort: fails silently
}
}
}
}