From bf2d436fefd2750320fa704eef45a6dabf08dd10 Mon Sep 17 00:00:00 2001 From: VladMordock Date: Wed, 17 Feb 2021 18:10:19 +0100 Subject: [PATCH] Improved logging --- 02_Windows_App/Perun_v1/01_Classes/Globals.cs | 1 - .../Perun_v1/01_Classes/LogController.cs | 36 ++----------------- .../Perun_v1/01_Classes/PerunHelper.cs | 22 ++++++++++-- .../Perun_v1/02_Forms/form_Main.Designer.cs | 17 ++++----- .../Perun_v1/02_Forms/form_Main.resx | 3 -- 5 files changed, 30 insertions(+), 49 deletions(-) diff --git a/02_Windows_App/Perun_v1/01_Classes/Globals.cs b/02_Windows_App/Perun_v1/01_Classes/Globals.cs index 5b21886..bc5112b 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Globals.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Globals.cs @@ -9,7 +9,6 @@ class HardwareMonitorClass protected PerformanceCounter cpuCounter; protected PerformanceCounter ramCounter; - protected PerformanceCounter cputempCounter; public HardwareMonitorClass() { diff --git a/02_Windows_App/Perun_v1/01_Classes/LogController.cs b/02_Windows_App/Perun_v1/01_Classes/LogController.cs index 2eb81fc..177bc12 100644 --- a/02_Windows_App/Perun_v1/01_Classes/LogController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/LogController.cs @@ -5,6 +5,7 @@ using System.IO; class LogController { private static LogController _instance = new LogController(); // Singleton instance + public int level; // Level of logging public static LogController instance { @@ -14,40 +15,9 @@ class LogController } } - public int level - { - get; // Get debug level - set; // Set debug level - } - - // Log error information - public void LogError(string strLog) - { - this.WriteLog(0, strLog); - } - - // Log warning information - public void LogWarning(string strLog) - { - this.WriteLog(1, strLog); - } - - // Log info information - public void LogInfo(string strLog) - { - this.WriteLog(2, strLog); - } - - // Log debug information - public void LogDebug(string strLog) - { - this.WriteLog(3, strLog); - } - - // TBD - done via https://stackoverflow.com/questions/20185015/how-to-write-log-file-in-c public void WriteLog(int logLevel, string strLog) { - if (logLevel > this.level) return; + if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit StreamWriter LogStreamWriter; FileStream LogFileStream = null; @@ -55,7 +25,7 @@ class LogController FileInfo LogFileInfo; string LogFilePath = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Perun\\"; - LogFilePath = LogFilePath + "Perun_Log_" + System.DateTime.Today.ToString("yyyyddMM") + "." + "txt"; + LogFilePath = LogFilePath + "Perun_Log_" + Globals.AppInstanceID + "_" + System.DateTime.Today.ToString("yyyyddMM") + "." + "txt"; LogFileInfo = new FileInfo(LogFilePath); LogDirectoryInfo = new DirectoryInfo(LogFileInfo.DirectoryName); if (!LogDirectoryInfo.Exists) LogDirectoryInfo.Create(); diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs index 3f28238..b3afecb 100644 --- a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -48,7 +48,25 @@ internal class PerunHelper LogDirection = "^"; break; default: - LogDirection = " "; + LogDirection = "-"; + break; + } + + // Insert information about which log type we are curently handling + string LogType; + switch (logLevel) + { + case 0: + LogType = "E"; + break; + case 1: + LogType = "W"; + break; + case 2: + LogType = "I"; + break; + default: + LogType = "D"; break; } @@ -67,7 +85,7 @@ internal class PerunHelper } // Add new entry - arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " " + LogDirection + " " + strEntryToAdd; // Add entry at the last position + arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " " + LogDirection + " " + LogType + " " + strEntryToAdd; // Add entry at the last position // Update control at my window Globals.AppUpdateGUI = true; diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs index bd02961..e42b4f4 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs @@ -100,7 +100,6 @@ this.label27 = new System.Windows.Forms.Label(); this.label20 = new System.Windows.Forms.Label(); this.tim_HW_status = new System.Windows.Forms.Timer(this.components); - this.performanceCounter1 = new System.Diagnostics.PerformanceCounter(); this.con_GroupBox_1.SuspendLayout(); this.con_GroupBox_2.SuspendLayout(); this.con_GroupBox_3.SuspendLayout(); @@ -114,7 +113,6 @@ this.con_GroupBox_5.SuspendLayout(); this.con_GroupBox_6.SuspendLayout(); this.con_GroupBox_7.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.performanceCounter1)).BeginInit(); this.SuspendLayout(); // // con_List_Received @@ -123,14 +121,14 @@ this.con_List_Received.FormattingEnabled = true; this.con_List_Received.Items.AddRange(new object[] { "Not connected"}); - this.con_List_Received.Location = new System.Drawing.Point(8, 57); + this.con_List_Received.Location = new System.Drawing.Point(8, 44); this.con_List_Received.Name = "con_List_Received"; - this.con_List_Received.Size = new System.Drawing.Size(307, 108); + this.con_List_Received.Size = new System.Drawing.Size(307, 134); this.con_List_Received.TabIndex = 0; // // con_Button_Listen_ON // - this.con_Button_Listen_ON.Location = new System.Drawing.Point(342, 430); + this.con_Button_Listen_ON.Location = new System.Drawing.Point(342, 441); this.con_Button_Listen_ON.Name = "con_Button_Listen_ON"; this.con_Button_Listen_ON.Size = new System.Drawing.Size(86, 39); this.con_Button_Listen_ON.TabIndex = 2; @@ -141,7 +139,7 @@ // con_Button_Listen_OFF // this.con_Button_Listen_OFF.Enabled = false; - this.con_Button_Listen_OFF.Location = new System.Drawing.Point(434, 430); + this.con_Button_Listen_OFF.Location = new System.Drawing.Point(434, 441); this.con_Button_Listen_OFF.Name = "con_Button_Listen_OFF"; this.con_Button_Listen_OFF.Size = new System.Drawing.Size(86, 39); this.con_Button_Listen_OFF.TabIndex = 3; @@ -157,7 +155,7 @@ this.con_GroupBox_1.Controls.Add(this.con_Button_Add_Marker); this.con_GroupBox_1.Location = new System.Drawing.Point(342, 248); this.con_GroupBox_1.Name = "con_GroupBox_1"; - this.con_GroupBox_1.Size = new System.Drawing.Size(324, 179); + this.con_GroupBox_1.Size = new System.Drawing.Size(324, 187); this.con_GroupBox_1.TabIndex = 4; this.con_GroupBox_1.TabStop = false; this.con_GroupBox_1.Text = "Data log"; @@ -357,7 +355,7 @@ // // con_Button_Quit // - this.con_Button_Quit.Location = new System.Drawing.Point(580, 430); + this.con_Button_Quit.Location = new System.Drawing.Point(580, 441); this.con_Button_Quit.Name = "con_Button_Quit"; this.con_Button_Quit.Size = new System.Drawing.Size(86, 39); this.con_Button_Quit.TabIndex = 7; @@ -808,7 +806,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(675, 479); + this.ClientSize = new System.Drawing.Size(675, 485); this.Controls.Add(this.con_GroupBox_7); this.Controls.Add(this.con_GroupBox_6); this.Controls.Add(this.con_GroupBox_5); @@ -932,7 +930,6 @@ private System.Windows.Forms.Label label27; private System.Windows.Forms.Label label20; private System.Windows.Forms.Timer tim_HW_status; - private System.Diagnostics.PerformanceCounter performanceCounter1; } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.resx b/02_Windows_App/Perun_v1/02_Forms/form_Main.resx index c04f311..e2aa840 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.resx +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.resx @@ -425,9 +425,6 @@ 800, 4 - - 800, 19 - 26