ErrorLogger.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Optimizer {
  5. internal static class Logger {
  6. internal static string ErrorLogFile = Path.Combine(CoreHelper.CoreFolder, "Optimizer.log");
  7. static StringBuilder _silentReportLog;
  8. private static void LogErrorSilent(string functionName, string errorMessage, string errorStackTrace) {
  9. _silentReportLog.AppendLine(string.Format("[ERROR] [{0}] in function [{1}]", DateTime.Now.ToString(), functionName));
  10. _silentReportLog.AppendLine();
  11. _silentReportLog.AppendLine(errorMessage);
  12. _silentReportLog.AppendLine();
  13. _silentReportLog.AppendLine(errorStackTrace);
  14. _silentReportLog.AppendLine();
  15. _silentReportLog.AppendLine();
  16. }
  17. internal static void LogInfoSilent(string message) {
  18. _silentReportLog.AppendLine($"[OK] {message}");
  19. _silentReportLog.AppendLine();
  20. }
  21. internal static void InitializeSilentReport() {
  22. _silentReportLog = new StringBuilder();
  23. _silentReportLog.AppendLine(Utilities.GetWindowsDetails());
  24. _silentReportLog.AppendLine(string.Format("Optimizer {0} - .NET Framework {1} - Experimental build: {2}", Program.GetCurrentVersionTostring(), Utilities.GetNETFramework(), Program.EXPERIMENTAL_BUILD));
  25. _silentReportLog.AppendLine($"{DateTime.Now.ToLongDateString()} - {DateTime.Now.ToLongTimeString()}");
  26. _silentReportLog.AppendLine();
  27. _silentReportLog.AppendLine();
  28. }
  29. internal static void GenerateSilentReport() {
  30. try {
  31. File.WriteAllText($"Optimizer.SilentReport.{DateTime.Now.ToString("yyyyMMddTHHmm")}.log", _silentReportLog.ToString());
  32. }
  33. catch { }
  34. }
  35. internal static void LogError(string functionName, string errorMessage, string errorStackTrace) {
  36. if (Program.SILENT_MODE) {
  37. LogErrorSilent(functionName, errorMessage, errorStackTrace);
  38. return;
  39. }
  40. try {
  41. if (!File.Exists(ErrorLogFile) || (File.Exists(ErrorLogFile) && File.ReadAllText(ErrorLogFile).Trim() == string.Empty)) {
  42. File.AppendAllText(ErrorLogFile, Utilities.GetWindowsDetails());
  43. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  44. File.AppendAllText(ErrorLogFile, string.Format("Optimizer {0} - .NET Framework {1} - Experimental build: {2}", Program.GetCurrentVersionTostring(), Utilities.GetNETFramework(), Program.EXPERIMENTAL_BUILD));
  45. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  46. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  47. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  48. }
  49. File.AppendAllText(ErrorLogFile, string.Format("[ERROR] [{0}] in function [{1}]", DateTime.Now.ToString(), functionName));
  50. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  51. File.AppendAllText(ErrorLogFile, errorMessage);
  52. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  53. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  54. File.AppendAllText(ErrorLogFile, errorStackTrace);
  55. // seperator
  56. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  57. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  58. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  59. }
  60. catch { }
  61. //finally
  62. //{
  63. // if (!Options.CurrentOptions.DisableOptimizerTelemetry)
  64. // {
  65. // TelemetryHelper.GenerateTelemetryData(functionName, errorMessage, errorStackTrace);
  66. // }
  67. //}
  68. }
  69. }
  70. }