Esempio n. 1
0
    private static void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
    {
        var e = args.Exception;

        string directoryPath = Path.Combine(Environment.CurrentDirectory, "Logs");
        string filePath      = Path.Combine(directoryPath, $"CrashLog {DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss")}.txt");

        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        using (var writer = new StreamWriter(filePath))
        {
            writer.WriteLine($"Exception message: {e.Message}");
            writer.WriteLine($"Inner exception: {e.InnerException}");
            writer.WriteLine($"Stack trace: {e.StackTrace}");
            writer.WriteLine();
            writer.WriteLine("User.json file:");

            //  If exception was thrown before loading User.json (Ingots.Count can't be null after loading)
            if (User.Instance.Ingots?.Count == 0)
            {
                var encryptedUserJson = File.ReadAllBytes(UserDataHelper.UserDataPath);
                var userJson          = EncryptionHelper.DecryptJsonUsingAes(encryptedUserJson);
                writer.Write(userJson);
            }
            else
            {
                writer.Write(UserDataHelper.Save());
            }
        }

        AlertBox.Show("There was an error with the game. A log file has been generated in the Logs folder. To help us fix the issue, you can contribute the log file at X. We apologize for the inconvenience.\nYour data has been saved.", MessageBoxButton.OK);
    }