ErrorLogger.cs 3.9 KB

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