New feature - added simple logging to file

This commit is contained in:
szporowolik
2019-10-19 21:31:17 +02:00
parent 20d19b2670
commit 48f60abbfa
6 changed files with 51 additions and 10 deletions

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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)

View File

@@ -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++)