Quellcode durchsuchen

Completed internal logging and tested logging.

Gregory vor 3 Jahren
Ursprung
Commit
c623306858
3 geänderte Dateien mit 118 neuen und 27 gelöschten Zeilen
  1. 15 12
      crusherScanner/Form1.Designer.cs
  2. 17 0
      crusherScanner/Form1.cs
  3. 86 15
      crusherScanner/Logging.cs

+ 15 - 12
crusherScanner/Form1.Designer.cs

@@ -120,7 +120,7 @@
             this.toolStripSeparator5,
             this.purgeToolStripMenuItem1});
             this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
-            this.settingsToolStripMenuItem.Size = new System.Drawing.Size(195, 34);
+            this.settingsToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.settingsToolStripMenuItem.Text = "Settings";
             // 
             // editToolStripMenuItem
@@ -164,7 +164,7 @@
             // toolStripSeparator2
             // 
             this.toolStripSeparator2.Name = "toolStripSeparator2";
-            this.toolStripSeparator2.Size = new System.Drawing.Size(192, 6);
+            this.toolStripSeparator2.Size = new System.Drawing.Size(267, 6);
             // 
             // logsToolStripMenuItem
             // 
@@ -173,31 +173,34 @@
             this.openDirectoryToolStripMenuItem,
             this.purgeToolStripMenuItem});
             this.logsToolStripMenuItem.Name = "logsToolStripMenuItem";
-            this.logsToolStripMenuItem.Size = new System.Drawing.Size(195, 34);
+            this.logsToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.logsToolStripMenuItem.Text = "Logs";
             // 
             // openCurrentToolStripMenuItem
             // 
             this.openCurrentToolStripMenuItem.Name = "openCurrentToolStripMenuItem";
-            this.openCurrentToolStripMenuItem.Size = new System.Drawing.Size(233, 34);
+            this.openCurrentToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.openCurrentToolStripMenuItem.Text = "Open current";
+            this.openCurrentToolStripMenuItem.Click += new System.EventHandler(this.openCurrentToolStripMenuItem_Click);
             // 
             // openDirectoryToolStripMenuItem
             // 
             this.openDirectoryToolStripMenuItem.Name = "openDirectoryToolStripMenuItem";
-            this.openDirectoryToolStripMenuItem.Size = new System.Drawing.Size(233, 34);
+            this.openDirectoryToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.openDirectoryToolStripMenuItem.Text = "Open directory";
+            this.openDirectoryToolStripMenuItem.Click += new System.EventHandler(this.openDirectoryToolStripMenuItem_Click);
             // 
             // purgeToolStripMenuItem
             // 
             this.purgeToolStripMenuItem.Name = "purgeToolStripMenuItem";
-            this.purgeToolStripMenuItem.Size = new System.Drawing.Size(233, 34);
+            this.purgeToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.purgeToolStripMenuItem.Text = "Purge";
+            this.purgeToolStripMenuItem.Click += new System.EventHandler(this.purgeToolStripMenuItem_Click);
             // 
             // toolStripSeparator3
             // 
             this.toolStripSeparator3.Name = "toolStripSeparator3";
-            this.toolStripSeparator3.Size = new System.Drawing.Size(192, 6);
+            this.toolStripSeparator3.Size = new System.Drawing.Size(267, 6);
             // 
             // reinitializeToolStripMenuItem
             // 
@@ -205,7 +208,7 @@
             this.reinitializeSettingsToolStripMenuItem,
             this.testSettingsToolStripMenuItem});
             this.reinitializeToolStripMenuItem.Name = "reinitializeToolStripMenuItem";
-            this.reinitializeToolStripMenuItem.Size = new System.Drawing.Size(195, 34);
+            this.reinitializeToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.reinitializeToolStripMenuItem.Text = "Reinitialize";
             // 
             // reinitializeSettingsToolStripMenuItem
@@ -224,12 +227,12 @@
             // toolStripSeparator4
             // 
             this.toolStripSeparator4.Name = "toolStripSeparator4";
-            this.toolStripSeparator4.Size = new System.Drawing.Size(192, 6);
+            this.toolStripSeparator4.Size = new System.Drawing.Size(267, 6);
             // 
             // ExitToolStripMenuItem
             // 
             this.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem";
-            this.ExitToolStripMenuItem.Size = new System.Drawing.Size(195, 34);
+            this.ExitToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
             this.ExitToolStripMenuItem.Text = "Exit";
             this.ExitToolStripMenuItem.Click += new System.EventHandler(this.ExitToolStripMenuItem_Click);
             // 
@@ -331,7 +334,7 @@
             this.toolStripStatusLabel5,
             this.toolStripStatusLabel7,
             this.toolStripProgressBar1});
-            this.statusStrip1.Location = new System.Drawing.Point(0, 1128);
+            this.statusStrip1.Location = new System.Drawing.Point(0, 1018);
             this.statusStrip1.Name = "statusStrip1";
             this.statusStrip1.Size = new System.Drawing.Size(1708, 32);
             this.statusStrip1.TabIndex = 9;
@@ -398,7 +401,7 @@
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(1708, 1160);
+            this.ClientSize = new System.Drawing.Size(1708, 1050);
             this.Controls.Add(this.label8);
             this.Controls.Add(this.statusStrip1);
             this.Controls.Add(this.label7);

+ 17 - 0
crusherScanner/Form1.cs

@@ -12,6 +12,7 @@ namespace crusherScanner
 
         private void Form1_Load(object sender, EventArgs e)
         {
+            Logging.Append("Crusher Scanner Appliction " + Application.ProductVersion + " is starting.");
             TextBox1.Enabled = false;
             toolStripStatusLabel5.Text = "";
             toolStripStatusLabel2.Text = "0";
@@ -28,6 +29,7 @@ namespace crusherScanner
             dataCore = new ProgramFunctions();
             TextBox1.Enabled = true;
             TextBox1.Focus();
+            Logging.Append("Crusher Scanner Appliction " + Application.ProductVersion + " has started.");
         }
 
         private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
@@ -229,5 +231,20 @@ namespace crusherScanner
                 toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
             }
         }
+
+        private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            Logging.OpenLogDirectory(); 
+        }
+
+        private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            Logging.OpenInTextEditor();
+        }
+
+        private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            Logging.PurgeLogs();
+        }
     }
 }

+ 86 - 15
crusherScanner/Logging.cs

@@ -1,43 +1,114 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.Diagnostics;
 
 namespace crusherScanner
 {
-    internal class Logging
+    /// <summary>
+    /// Log file handler.
+    /// </summary>
+    internal static class Logging
     {
 
-        private static readonly string LocalLogDir = Environment.SpecialFolder.LocalApplicationData.ToString() + "\\crusherScanner";
+        private static string logFile = "";
+        private static readonly string LocalLogDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\crusherScanner";
 
-        public void Append()
+        /// <summary>
+        /// Create/Append the current log file.
+        /// </summary>
+        /// <param name="message">Message to be written into the log file.</param>
+        /// <returns></returns>
+        public static bool Append(string message)
         {
+            var dt = DateTime.Now;
+            if (!Directory.Exists(LocalLogDir))
+            {
+                try
+                {
+                    Directory.CreateDirectory(LocalLogDir);
+                }
+                catch (Exception ex)
+                {
+                    MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    return false;
+                }
+            }
+
+            // if no log file for this instance create one
+            if (logFile == "")
+            {
+                logFile = dt.ToString("yyyyMMdd") + "-0.log";
+                for (int i = 1; i < 100; i++)
+                {
+                    // if an old log file is detected create a new one for this instance.
+                    if (File.Exists(LocalLogDir + "\\" + logFile))
+                    {
+                        logFile = dt.ToString("yyyyMMdd") + "-" + i + ".log";
+                    }
+                    else
+                    {
+                        break;
+                    }
+                }
+            }
+
+            try
+            {
 
+                File.AppendAllText(LocalLogDir + "\\" + logFile, dt.ToString("yyyyMMddTHHmmss") + "---" + message + "\n");
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message, "Error in logging!", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                return false;
+            }
+            return true;
         }
 
-        public void OpenInTextEditor()
+        /// <summary>
+        /// Open the current log file in the system default text editor.
+        /// </summary>
+        public static void OpenInTextEditor()
         {
-
+            StartProcess(LocalLogDir + "\\" + logFile);
         }
 
-        public void OpenLogDirectory()
+        /// <summary>
+        /// Open the directroy that contains the log files.
+        /// </summary>
+        public static void OpenLogDirectory()
         {
-            Process.Start(LocalLogDir);
+            StartProcess(LocalLogDir);
         }
 
+        /// <summary>
+        /// Remove all log files from the log directory.
+        /// </summary>
         public static void PurgeLogs()
         {
-            DialogResult result = MessageBox.Show("Are you sure you want to delete ALL logs?","Purge Logs",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation);
+            DialogResult result = MessageBox.Show("Are you sure you want to delete ALL logs?", "Purge Logs", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
             if (result == DialogResult.Yes)
             {
-                var logs = Directory.EnumerateFiles(LocalLogDir);
+                var logs = Directory.EnumerateFiles(LocalLogDir, "*-*.log");
                 foreach (var log in logs)
                 {
                     File.Delete(log);
                 }
             }
         }
+
+        /// <summary>
+        /// Pass a location or file to the operating system to open.
+        /// </summary>
+        /// <param name="target">Location or file to open.</param>
+        private static void StartProcess(string target)
+        {
+            new Process
+            {
+                StartInfo = new ProcessStartInfo(target)
+                {
+                    UseShellExecute = true
+                }
+            }.Start();
+        }
+
     }
 }