-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (120 loc) · 4.81 KB
/
Copy pathProgram.cs
File metadata and controls
144 lines (120 loc) · 4.81 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
using Avalonia;
using Serilog;
using Serilog.Events;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Yellowcake.Services;
namespace Yellowcake;
class Program
{
[STAThread]
public static void Main(string[] args)
{
File.WriteAllText("startup.log", $"App started at {DateTime.Now}\n");
try
{
ConfigureLogging();
Log.Information("Starting Avalonia application...");
Log.Debug("Platform: {Platform}", Environment.OSVersion);
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
// Cross-platform error handling
HandleFatalError(ex);
Environment.Exit(1);
}
finally
{
Log.CloseAndFlush();
}
}
private static void ConfigureLogging()
{
var logPath = Path.Combine(AppContext.BaseDirectory, "logs");
Directory.CreateDirectory(logPath);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Avalonia", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(
Path.Combine(logPath, "yellowcake-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] {Message:lj}{NewLine}{Exception}",
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1))
.WriteTo.Sink(InMemorySink.Instance)
.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
Log.Information("=== Starting ===");
Log.Information("Application Version: {Version}",
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Log.Information("OS: {OS}", Environment.OSVersion);
Log.Information(".NET Version: {Version}", Environment.Version);
Log.Information("Platform: {Platform}", GetPlatformName());
Log.Information("Base Directory: {Directory}", AppContext.BaseDirectory);
}
public static AppBuilder BuildAvaloniaApp()
{
try
{
Log.Debug("Configuring Avalonia AppBuilder...");
var builder = AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace(Avalonia.Logging.LogEventLevel.Warning);
Log.Debug("Avalonia AppBuilder configured successfully");
return builder;
}
catch (Exception ex)
{
Log.Fatal(ex, "Failed to configure Avalonia AppBuilder");
throw;
}
}
private static void HandleFatalError(Exception ex)
{
var errorMessage = $"""
================================================================================
FATAL ERROR - Yellowcake Mod Manager
================================================================================
The application encountered a fatal error and must close.
Error: {ex.Message}
Stack Trace:
{ex.StackTrace}
Log Location: {Path.Combine(AppContext.BaseDirectory, "logs")}
Please check the log files for more details.
================================================================================
""";
// Write to console (cross-platform)
Console.Error.WriteLine(errorMessage);
// Write to a crash report file
try
{
var crashReportPath = Path.Combine(AppContext.BaseDirectory, $"crash-{DateTime.Now:yyyyMMdd-HHmmss}.log");
File.WriteAllText(crashReportPath, $"{errorMessage}\n\nFull Exception:\n{ex}");
Console.Error.WriteLine($"\nCrash report saved to: {crashReportPath}");
}
catch
{
// Ignore errors writing crash report
}
}
private static string GetPlatformName()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return $"Windows {Environment.OSVersion.Version}";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return $"Linux {Environment.OSVersion.Version}";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return $"macOS {Environment.OSVersion.Version}";
return $"Unknown ({RuntimeInformation.OSDescription})";
}
}