Logging.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Diagnostics;
  2. namespace crusherScanner
  3. {
  4. /// <summary>
  5. /// Log file handler.
  6. /// </summary>
  7. internal static class Logging
  8. {
  9. private static string logFile = "";
  10. private static readonly string LocalLogDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\crusherScanner";
  11. /// <summary>
  12. /// Create/Append the current log file.
  13. /// </summary>
  14. /// <param name="message">Message to be written into the log file.</param>
  15. /// <returns></returns>
  16. public static bool Append(string message)
  17. {
  18. var dt = DateTime.Now;
  19. if (!Directory.Exists(LocalLogDir))
  20. {
  21. try
  22. {
  23. Directory.CreateDirectory(LocalLogDir);
  24. }
  25. catch (Exception ex)
  26. {
  27. MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  28. return false;
  29. }
  30. }
  31. // if no log file for this instance create one
  32. if (logFile == "")
  33. {
  34. logFile = dt.ToString("yyyyMMdd") + "-0.log";
  35. for (int i = 1; i < 100; i++)
  36. {
  37. // if an old log file is detected create a new one for this instance.
  38. if (File.Exists(LocalLogDir + "\\" + logFile))
  39. {
  40. logFile = dt.ToString("yyyyMMdd") + "-" + i + ".log";
  41. }
  42. else
  43. {
  44. break;
  45. }
  46. }
  47. }
  48. try
  49. {
  50. File.AppendAllText(LocalLogDir + "\\" + logFile, dt.ToString("yyyyMMddTHHmmss") + "---" + message + "\n");
  51. }
  52. catch (Exception ex)
  53. {
  54. MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  55. return false;
  56. }
  57. return true;
  58. }
  59. /// <summary>
  60. /// Open the current log file in the system default text editor.
  61. /// </summary>
  62. public static void OpenInTextEditor()
  63. {
  64. StartProcess(LocalLogDir + "\\" + logFile);
  65. }
  66. /// <summary>
  67. /// Open the directroy that contains the log files.
  68. /// </summary>
  69. public static void OpenLogDirectory()
  70. {
  71. StartProcess(LocalLogDir);
  72. }
  73. /// <summary>
  74. /// Remove all log files from the log directory.
  75. /// </summary>
  76. public static void PurgeLogs()
  77. {
  78. DialogResult result = MessageBox.Show("Are you sure you want to delete ALL logs?", "Purge Logs", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
  79. if (result == DialogResult.Yes)
  80. {
  81. var logs = Directory.EnumerateFiles(LocalLogDir, "*-*.log");
  82. foreach (var log in logs)
  83. {
  84. File.Delete(log);
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Pass a location or file to the operating system to open.
  90. /// </summary>
  91. /// <param name="target">Location or file to open.</param>
  92. private static void StartProcess(string target)
  93. {
  94. new Process
  95. {
  96. StartInfo = new ProcessStartInfo(target)
  97. {
  98. UseShellExecute = true
  99. }
  100. }.Start();
  101. }
  102. }
  103. }