using System; using System.IO; using System.Windows.Forms; namespace AdvertsingProfitControl { internal class GlobalClasses { public static string LogFilePath = Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\"; public GlobalClasses() { //Check to make sure the directory exists and if it doesn't then create it. if (Directory.Exists(Logger.LogFolderPath)) return; try { Directory.CreateDirectory(Logger.LogFolderPath); } catch (IOException e) { MessageBox.Show(e.Message + Environment.NewLine + @"Advertising Profit Control will be forced to exit.", @"Failed to Create Log File Directory", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// Logs any error related to uncaught thread exceptions. /// /// /// public static void LogError(object sender, System.Threading.ThreadExceptionEventArgs e) { using ( var file = new StreamWriter(Logger.LogFolderPath + DateTime.Now.ToString("yy-MM-dd") + ".log", true)) { file.WriteLine(DateTime.Now + ": Unhandled Thread Exception:\n" + e.Exception.Message + Environment.NewLine); MessageBox.Show(@"An unhandled thread exception has occurred. Advertising Profit Control will be forced to exit.", @"Fatal Error"); Application.Exit(); } } /// /// Logs any unhandled exceptions that fires. /// /// /// public static void LogError(object sender, UnhandledExceptionEventArgs e) { using (var file = new StreamWriter(Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\" + DateTime.Now.ToString("yy-MM-dd") + ".log", true)) { file.WriteLine(DateTime.Now + ": An unhandled exception occurred. \nException Object: " + e.ExceptionObject + Environment.NewLine); MessageBox.Show(@"An unknown error has occurred. Advertising Profit Control will be forced to exit.", @"Fatal Error"); Application.Exit(); } } /// /// Used to log anything from information for the user to errors that don't cause a total application failure. /// /// The message to log for future use. public static void WriteToLog(string message) { using ( var file = new StreamWriter(Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\Information.log", true)) { file.WriteLine(message); } } } }