Files
GameLauncher/App.xaml.cs
T

53 lines
1.9 KiB
C#

using System;
using System.IO;
using System.Windows;
using GameLauncher.Helpers;
namespace GameLauncher
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// 作業ディレクトリを exe のあるフォルダに確実に設定
AppPathHelper.SetCurrentDirectoryToExeDirectory();
// 未処理例外をグローバルに捕捉してサイレント終了を防ぐ
DispatcherUnhandledException += (s, args) =>
{
var ex = args.Exception;
LogError("DispatcherUnhandledException", ex);
MessageBox.Show($"アプリケーションエラーが発生しました:\n\n{ex.Message}\n\n詳細:\n{ex}",
"GameLauncher エラー", MessageBoxButton.OK, MessageBoxImage.Error);
args.Handled = true;
};
AppDomain.CurrentDomain.UnhandledException += (s, args) =>
{
if (args.ExceptionObject is Exception ex)
{
LogError("AppDomain.UnhandledException", ex);
MessageBox.Show($"致命的なエラーが発生しました:\n\n{ex.Message}\n\n詳細:\n{ex}",
"GameLauncher 致命的エラー", MessageBoxButton.OK, MessageBoxImage.Error);
}
};
}
private static void LogError(string source, Exception ex)
{
try
{
var baseDir = AppPathHelper.GetExeDirectory();
var logFile = Path.Combine(baseDir, "error.log");
File.AppendAllText(logFile, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{source}] {ex}\n\n");
}
catch { }
}
}
}