diff --git a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs index b82b003..2761aef 100644 --- a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs @@ -22,11 +22,24 @@ public int SendToMySql(string RawTCPFrame, bool CheckConnection = false) } // Parse frame - dynamic TCPFrame = JsonConvert.DeserializeObject(RawTCPFrame); // Deserialize raw data - string TCPFrameType = TCPFrame.type; // Frame type - string TCPFrameTimestamp = (TCPFrame.timestamp != null) ? $"'{TCPFrame.timestamp}'" : "CURRENT_TIMESTAMP()"; // Frame timestamp (Some frames may come without timestamp, use database timestamp then) - string TCPFrameInstance = TCPFrame.instance; // Frame instance - string TCPFramePayload; // Frame payload (we will deserialise only for specific frame types) + dynamic TCPFrame; + string TCPFrameType; + string TCPFrameTimestamp; + string TCPFrameInstance; + try + { + TCPFrame = JsonConvert.DeserializeObject(RawTCPFrame); // Deserialize raw data + TCPFrameType = TCPFrame.type; + TCPFrameTimestamp = (TCPFrame.timestamp != null) ? $"'{TCPFrame.timestamp}'" : "CURRENT_TIMESTAMP()"; // Frame timestamp (Some frames may come without timestamp, use database timestamp then) + TCPFrameInstance = TCPFrame.instance; + } + catch (Exception) + { + LogController.instance.LogError("ERROR - SendToMySql cannot deserialize tcp frame !", RawTCPFrame); + return 0; + } + + string TCPFramePayload; // Frame payload (we will deserialise only for specific frame types) // Connect to mysql and execute sql try diff --git a/02_Windows_App/Perun_v1/01_Classes/LogController.cs b/02_Windows_App/Perun_v1/01_Classes/LogController.cs index 9dad7c1..c78f82d 100644 --- a/02_Windows_App/Perun_v1/01_Classes/LogController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/LogController.cs @@ -15,7 +15,28 @@ public static LogController instance } } - public void WriteLog(int logLevel, string strLog) + public void LogError(string strLog, string content = null) + { + this.WriteLog(0, strLog, content); + } + + public void LogWarning(string strLog, string content = null) + { + this.WriteLog(1, strLog, content); + } + + public void LogInfo(string strLog, string content = null) + { + this.WriteLog(2, strLog, content); + } + + public void LogDebug(string strLog, string content = null) + { + this.WriteLog(3, strLog, content); + } + + // TBD - done via https://stackoverflow.com/questions/20185015/how-to-write-log-file-in-c + public void WriteLog(int logLevel, string strLog, string content = null) { // Write log entry to file if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit @@ -25,6 +46,7 @@ public void WriteLog(int logLevel, string strLog) FileStream LogFileStream = null; DirectoryInfo LogDirectoryInfo = null; FileInfo LogFileInfo; + string message = strLog; string LogFileDir = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Perun\\"; string LogFilePath = LogFileDir + "Perun_Log_" + Globals.AppInstanceID + "_" + System.DateTime.Today.ToString("yyyyMMdd") + "." + "log"; @@ -46,10 +68,19 @@ public void WriteLog(int logLevel, string strLog) // Do nothing - TBD error handling } } + try { + if (content != null) + { + // write the content to a file and add the name of this file to the message + string contentFilename = "Perun_LogContent_" + System.DateTime.Today.ToString("o").Replace('T', '-').Replace(':', '-').Replace('+','-') + "." + "txt"; + string contentFilepath = LogFileInfo + contentFilename; + File.WriteAllText(contentFilepath, content); + message = strLog + " - content stored in " + contentFilename; + } LogStreamWriter = new StreamWriter(LogFileStream); - LogStreamWriter.WriteLine(strLog); + LogStreamWriter.WriteLine(message); LogStreamWriter.Close(); Globals.LastLogLocation = LogFilePath; } diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs index f129ed6..09d760d 100644 --- a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -43,8 +43,7 @@ public static void LogDebug(ref string[] arrLogHistory, string strEntryToAdd, in AddLog(3, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui); } - // Add log - private static void AddLog(int logLevel, ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false) + private static void AddLog(int logLevel, ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false, string content = null) { // Declare values string LogDirection; @@ -106,7 +105,7 @@ private static void AddLog(int logLevel, ref string[] arrLogHistory, string strE Globals.AppUpdateGUI = true; } // Add the entry to log file - LogController.instance.WriteLog(logLevel, $"{DateTime.Now.ToString("yyyy-MM-dd ")} {DateTime.Now.ToString("HH:mm:ss.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.LastFrameTime.ToString("000000.")} | {Globals.LastFrameDelay.ToString("000000.")} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}"); + LogController.instance.WriteLog(logLevel, $"{DateTime.Now.ToString("yyyy-MM-dd ")} {DateTime.Now.ToString("HH:mm:ss.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.LastFrameTime.ToString("000000.")} | {Globals.LastFrameDelay.ToString("000000.")} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}", content); } // Gets build version