ErrorLogger.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Optimizer
  5. {
  6. internal static class ErrorLogger
  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 InitSilentReport()
  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();
  31. _silentReportLog.AppendLine();
  32. }
  33. internal static void GenerateSilentReport()
  34. {
  35. try
  36. {
  37. File.WriteAllText("optimizer-silent-report.log", _silentReportLog.ToString());
  38. }
  39. catch { }
  40. }
  41. internal static void LogError(string functionName, string errorMessage, string errorStackTrace)
  42. {
  43. if (Program.SILENT_MODE)
  44. {
  45. LogErrorSilent(functionName, errorMessage, errorStackTrace);
  46. return;
  47. }
  48. try
  49. {
  50. if (!File.Exists(ErrorLogFile) || (File.Exists(ErrorLogFile) && File.ReadAllText(ErrorLogFile).Trim() == string.Empty))
  51. {
  52. File.AppendAllText(ErrorLogFile, Utilities.GetWindowsDetails());
  53. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  54. File.AppendAllText(ErrorLogFile, string.Format("Optimizer {0} - .NET Framework {1} - Experimental build: {2}", Program.GetCurrentVersionTostring(), Utilities.GetNETFramework(), Program.EXPERIMENTAL_BUILD));
  55. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  56. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  57. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  58. }
  59. File.AppendAllText(ErrorLogFile, string.Format("[ERROR] [{0}] in function [{1}]", DateTime.Now.ToString(), functionName));
  60. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  61. File.AppendAllText(ErrorLogFile, errorMessage);
  62. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  63. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  64. File.AppendAllText(ErrorLogFile, errorStackTrace);
  65. // seperator
  66. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  67. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  68. File.AppendAllText(ErrorLogFile, Environment.NewLine);
  69. }
  70. catch { }
  71. //finally
  72. //{
  73. // if (!Options.CurrentOptions.DisableOptimizerTelemetry)
  74. // {
  75. // TelemetryHelper.GenerateTelemetryData(functionName, errorMessage, errorStackTrace);
  76. // }
  77. //}
  78. }
  79. }
  80. }