Logging.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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="level">Logging level.</param>
  15. /// <param name="message">Message to be written into the log file.</param>
  16. /// <returns></returns>
  17. public static bool Append(LogLevel level,string message)
  18. {
  19. // Invert logic, exit if loglevel not enabled.
  20. if(!LoggingLevel(level))
  21. {
  22. return true;
  23. }
  24. var dt = DateTime.Now;
  25. if (!Directory.Exists(LocalLogDir))
  26. {
  27. try
  28. {
  29. Directory.CreateDirectory(LocalLogDir);
  30. }
  31. catch (Exception ex)
  32. {
  33. MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  34. return false;
  35. }
  36. }
  37. // if no log file for this instance create one
  38. if (logFile == "")
  39. {
  40. logFile = dt.ToString("yyyyMMdd") + "-0.log";
  41. for (int i = 1; i < 100; i++)
  42. {
  43. // if an old log file is detected create a new one for this instance.
  44. if (File.Exists(LocalLogDir + "\\" + logFile))
  45. {
  46. logFile = dt.ToString("yyyyMMdd") + "-" + i + ".log";
  47. }
  48. else
  49. {
  50. break;
  51. }
  52. }
  53. }
  54. try
  55. {
  56. File.AppendAllText(LocalLogDir + "\\" + logFile, dt.ToString("yyyyMMddTHHmmss") + "-" + level.ToString() + "-" + message + "\n");
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  61. return false;
  62. }
  63. return true;
  64. }
  65. /// <summary>
  66. /// Open the current log file in the system default text editor.
  67. /// </summary>
  68. public static void OpenInTextEditor()
  69. {
  70. StartProcess(LocalLogDir + "\\" + logFile);
  71. }
  72. /// <summary>
  73. /// Open the directroy that contains the log files.
  74. /// </summary>
  75. public static void OpenLogDirectory()
  76. {
  77. StartProcess(LocalLogDir);
  78. }
  79. /// <summary>
  80. /// Remove all log files from the log directory.
  81. /// </summary>
  82. public static void PurgeLogs()
  83. {
  84. DialogResult result = MessageBox.Show("Are you sure you want to delete ALL logs?", "Purge Logs", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
  85. if (result == DialogResult.Yes)
  86. {
  87. var logs = Directory.EnumerateFiles(LocalLogDir, "*-*.log");
  88. foreach (var log in logs)
  89. {
  90. File.Delete(log);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// Pass a location or file to the operating system to open.
  96. /// </summary>
  97. /// <param name="target">Location or file to open.</param>
  98. private static void StartProcess(string target)
  99. {
  100. new Process
  101. {
  102. StartInfo = new ProcessStartInfo(target)
  103. {
  104. UseShellExecute = true
  105. }
  106. }.Start();
  107. }
  108. /// <summary>
  109. /// Is the logging for this message true;
  110. /// </summary>
  111. /// <param name="level">Loglevel for this message.</param>
  112. /// <returns></returns>
  113. private static bool LoggingLevel(LogLevel level)
  114. {
  115. if (Properties.Settings.Default.LoggingError && (level == LogLevel.Error))
  116. {
  117. return true;
  118. }
  119. if (Properties.Settings.Default.LoggingWarning && (level == LogLevel.Warning))
  120. {
  121. return true;
  122. }
  123. if (Properties.Settings.Default.LoggingInfo && (level == LogLevel.Information))
  124. {
  125. return true;
  126. }
  127. if (Properties.Settings.Default.LoggingDebug && (level == LogLevel.Debug))
  128. {
  129. return true;
  130. }
  131. return false;
  132. }
  133. }
  134. }