using System; using System.IO; using System.Windows.Forms; namespace AdvertsingProfitControl { internal class GlobalClasses { public static string LogFilePath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\APC\"; public static void LogError(object sender, System.Threading.ThreadExceptionEventArgs e) { if (!Directory.Exists(LogFilePath)) { Directory.CreateDirectory(LogFilePath); } using ( var file = new StreamWriter(@"C:\Users\" + Environment.UserName + @"\AppData\Local\APC\" + 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(); } } public static void LogError(object sender, UnhandledExceptionEventArgs e) { if (!Directory.Exists(LogFilePath)) { Directory.CreateDirectory(LogFilePath); } using (var file = new StreamWriter(@"C:\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(@"And unknown error has occurred. Advertising Profit Control will be forced to exit.", @"Fatal Error"); Application.Exit(); } } public static void WriteToLog(string message) { if (!Directory.Exists(LogFilePath)) { Directory.CreateDirectory(LogFilePath); } using ( var file = new StreamWriter(@"C:\Users\" + Environment.UserName + @"\AppData\Local\APC\Information.log", true)) { file.WriteLine(message); } } } }