private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // If we quit from the 'More Options' screen, apply the UI settings if (Content is MoreOptions) { ((MoreOptions)Content).ApplySettingsToUserSettings(); } // Write the settings ProjectFileInterface.WriteSettingsToFile(); // Check if any projects have incomplete logs if (UserSettings.DisplayIncompleteLogWarning) { foreach (var project in ProjectOrganizer.Projects) { // If the project has an incomplete log, ask the user if they want to complete it if (project.IncompleteLog != null) { ConfirmationDialog incLogDialog = new ConfirmationDialog(string.Format( "Project '{0}' has an incomplete log. Do you wish to complete it?", project.Name)); // If they do, finish it if (incLogDialog.ShowDialog() == true) { project.FinishIncompleteLog(); } } } } // Write the projects ProjectFileInterface.WriteAllProjectData(); // If errors occured, tell the user if (ErrorLogger.ErrorsOccured) { NotificationDialog window = new NotificationDialog("Errors Occured", string.Format("Errors occured during this session. Please view the log file at\n\n{0}\n\nto see which errors occurred.", ErrorLogger.ErrorLogFilename)); window.Show(); } }
//------------------// // External Methods // //------------------// /// <summary> /// Adds the given string to the log file. Automatically adds a timestamp. /// </summary> /// <param name="log">The log string to add.</param> public static void AddLog(string log, ErrorSeverity severity, [CallerMemberName] string currentFunction = "", [CallerFilePath] string currentFile = "", [CallerLineNumber] int line = 0) { try { // Note that errors have occured ErrorsOccured = true; // Get the date time as a string and the current file contents string dateTimeNowString = DateTime.Now.ToString(); string oldFileContents = File.ReadAllText(errorLogFilename); // Build the new file contents StringBuilder newFileContents = new StringBuilder(); string lineOfDashes = new String('-', 50); // Header newFileContents.Append(string.Format("{0}\n", lineOfDashes)); newFileContents.Append(dateTimeNowString); newFileContents.Append("\n"); newFileContents.Append(log); newFileContents.Append(string.Format("\nError Severity: {0}\n", errorSeverityToStringMap[severity])); // Immediate caller newFileContents.Append("\nImmediate Context:\n"); newFileContents.Append(string.Format(" Method {0}()\n", currentFunction)); newFileContents.Append(string.Format(" in {0}\n", currentFile)); newFileContents.Append(string.Format(" @ line {0}\n", line)); // Full callstack newFileContents.Append("\nCallstack:\n"); newFileContents.Append(Environment.StackTrace); // End newFileContents.Append(string.Format("\n{0}\n\n", lineOfDashes)); newFileContents.Append(oldFileContents); // Write the file File.WriteAllText(errorLogFilename, newFileContents.ToString()); // If we're in debug mode, pop up an error message if (UserSettings.DebugModeOn) { NotificationDialog window = new NotificationDialog("Error", string.Format("This program experienced an error:\n{0}", log)); window.Show(); } } catch (IOException e) { // If we get here, something went pretty wrong. Let the user know. NotificationDialog window = new NotificationDialog("Error", string.Format("This program encountered an error, and the error couldn't get logged.\n\nInitial error:\n{0}\n\nError log write error:\n{1}\n\n" + "If this problem persists, please contact us and let us know that it's happening.", log, e.Message)); window.Show(); } }