From 48f60abbfaa81323baaf63dabc33528a9631d0f2 Mon Sep 17 00:00:00 2001 From: szporowolik Date: Sat, 19 Oct 2019 21:31:17 +0200 Subject: [PATCH] New feature - added simple logging to file --- 02_Windows_App/Perun_v1/01_Classes/Globals.cs | 2 +- .../Perun_v1/01_Classes/LogController.cs | 35 +++++++++++++++++++ .../Perun_v1/01_Classes/PerunHelper.cs | 3 +- .../Perun_v1/01_Classes/TCPController.cs | 4 +-- 02_Windows_App/Perun_v1/02_Forms/form_Main.cs | 16 +++++---- 02_Windows_App/Perun_v1/Perun_v1.csproj | 1 + 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 02_Windows_App/Perun_v1/01_Classes/LogController.cs diff --git a/02_Windows_App/Perun_v1/01_Classes/Globals.cs b/02_Windows_App/Perun_v1/01_Classes/Globals.cs index 27b4bac..efa6ebe 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Globals.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Globals.cs @@ -7,7 +7,7 @@ internal class Globals public static string[] arrLogHistory = new string[10]; // Log history for GUI public static bool bLogHistoryUpdate = true; // Mark if log control require update public static string strPerunTitleText = ""; - + public static int intInstanceId = 0; // Instance ID public static bool bStatusIconsForce = true; // Force icons reload public static bool bdcConnection = false; // Historic db connection status public static bool btcpcServer = false; // Historic tcp connection status diff --git a/02_Windows_App/Perun_v1/01_Classes/LogController.cs b/02_Windows_App/Perun_v1/01_Classes/LogController.cs new file mode 100644 index 0000000..d1ba151 --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/LogController.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +class LogController +{ + // TBD - via https://stackoverflow.com/questions/20185015/how-to-write-log-file-in-c + public static void WriteLog(string strLog) + { + StreamWriter log; + FileStream fileStream = null; + DirectoryInfo logDirInfo = null; + FileInfo logFileInfo; + + string logFilePath = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Perun\\"; + logFilePath = logFilePath + "Perun_Log_" + System.DateTime.Today.ToString("yyyyddMM") + "." + "txt"; + logFileInfo = new FileInfo(logFilePath); + logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName); + if (!logDirInfo.Exists) logDirInfo.Create(); + if (!logFileInfo.Exists) + { + fileStream = logFileInfo.Create(); + } + else + { + fileStream = new FileStream(logFilePath, FileMode.Append); + } + log = new StreamWriter(fileStream); + log.WriteLine(strLog); + log.Close(); + } +} diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs index faafdd3..c6c1f0d 100644 --- a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -12,7 +12,8 @@ internal class PerunHelper arrLogHistory[i] = arrLogHistory[i + 1]; // Shift one down } - arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " > " + strEntryToAdd; // Add entry at the last position + arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("yyyy-dd-MM HH:mm:ss") + " > " + strEntryToAdd; // Add entry at the last position + LogController.WriteLog(arrLogHistory[arrLogHistory.Length - 1]); Globals.bLogHistoryUpdate = true; } public static string GetAppVersion(string strBeginning) diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index 7c682a8..49c11ca 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -16,7 +16,7 @@ public class TCPController public string[] arrSendBuffer; // Mysql send buffer public Thread thrTCPListener; // Seperate thread for TCP public bool bStatus; // TCP connecion status - + public void Create(int par_intListenPort, ref string[] par_arrLogHistory, ref string[] par_arrSendBuffer) { // Create class @@ -92,7 +92,7 @@ public class TCPController dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame string strRawTCPFrameType = dynamicRawTCPFrame.type; - PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType); + PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType); // Add to mySQL send buffer (find first empty slot) for (int i = 0; i < arrSendBuffer.Length - 1; i++) diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index cbcd6ea..5cbb18a 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -153,8 +153,12 @@ namespace Perun_v1 // ################################ User input ################################ private void con_Button_Listen_ON_Click(object sender, EventArgs e) { + // Set globals + Globals.intInstanceId= Int32.Parse(con_txt_dcs_instance.Text); + Globals.bStatusIconsForce = true; + // Start listening - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Opening connections"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Globals.intInstanceId + " > " + "Opening connections"); tcpcServer.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer); tcpcServer.thrTCPListener = new Thread(tcpcServer.StartListen); tcpcServer.thrTCPListener.Start(); @@ -166,9 +170,6 @@ namespace Perun_v1 // Prepare connection string dcConnection.strMySQLConnectionString = "server=" + con_txt_mysql_server.Text + ";user=" + con_txt_mysql_username.Text + ";database=" + con_txt_mysql_database.Text + ";port=" + con_txt_mysql_port.Text + ";password=" + con_txt_mysql_password.Text; - // Force icons update - Globals.bStatusIconsForce = true; - // Start timmers tim_200ms.Enabled = true; tim_1000ms.Enabled = true; @@ -186,7 +187,7 @@ namespace Perun_v1 // Stop listening // Display information - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Closing connections"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Globals.intInstanceId +" > " + "Closing connections"); con_Button_Listen_OFF.Enabled = false; timer1_Tick(null, null); this.Refresh(); @@ -219,12 +220,15 @@ namespace Perun_v1 tim_200ms.Enabled = false; // Display information about closed connections - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Connections closed"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Globals.intInstanceId +" > " + "Connections closed"); timer1_Tick(null, null); // Set title bar this.Text = Globals.strPerunTitleText; + // Set globals + Globals.intInstanceId = 0; + // Set helpers for updates Globals.bLogHistoryUpdate = false; Globals.bdcConnection = false; diff --git a/02_Windows_App/Perun_v1/Perun_v1.csproj b/02_Windows_App/Perun_v1/Perun_v1.csproj index 83f3640..2c076d4 100644 --- a/02_Windows_App/Perun_v1/Perun_v1.csproj +++ b/02_Windows_App/Perun_v1/Perun_v1.csproj @@ -124,6 +124,7 @@ +