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 string[] arrLogHistory = new string[10]; // Log history for GUI
public static bool bLogHistoryUpdate = true; // Mark if log control require update public static bool bLogHistoryUpdate = true; // Mark if log control require update
public static string strPerunTitleText = ""; public static string strPerunTitleText = "";
public static int intInstanceId = 0; // Instance ID
public static bool bStatusIconsForce = true; // Force icons reload public static bool bStatusIconsForce = true; // Force icons reload
public static bool bdcConnection = false; // Historic db connection status public static bool bdcConnection = false; // Historic db connection status
public static bool btcpcServer = false; // Historic tcp 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[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; Globals.bLogHistoryUpdate = true;
} }
public static string GetAppVersion(string strBeginning) public static string GetAppVersion(string strBeginning)

View File

@@ -92,7 +92,7 @@ public class TCPController
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type; 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) // Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++) for (int i = 0; i < arrSendBuffer.Length - 1; i++)

View File

@@ -153,8 +153,12 @@ namespace Perun_v1
// ################################ User input ################################ // ################################ User input ################################
private void con_Button_Listen_ON_Click(object sender, EventArgs e) 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 // 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.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer);
tcpcServer.thrTCPListener = new Thread(tcpcServer.StartListen); tcpcServer.thrTCPListener = new Thread(tcpcServer.StartListen);
tcpcServer.thrTCPListener.Start(); tcpcServer.thrTCPListener.Start();
@@ -166,9 +170,6 @@ namespace Perun_v1
// Prepare connection string // 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; 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 // Start timmers
tim_200ms.Enabled = true; tim_200ms.Enabled = true;
tim_1000ms.Enabled = true; tim_1000ms.Enabled = true;
@@ -186,7 +187,7 @@ namespace Perun_v1
// Stop listening // Stop listening
// Display information // Display information
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Closing connections"); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Globals.intInstanceId +" > " + "Closing connections");
con_Button_Listen_OFF.Enabled = false; con_Button_Listen_OFF.Enabled = false;
timer1_Tick(null, null); timer1_Tick(null, null);
this.Refresh(); this.Refresh();
@@ -219,12 +220,15 @@ namespace Perun_v1
tim_200ms.Enabled = false; tim_200ms.Enabled = false;
// Display information about closed connections // 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); timer1_Tick(null, null);
// Set title bar // Set title bar
this.Text = Globals.strPerunTitleText; this.Text = Globals.strPerunTitleText;
// Set globals
Globals.intInstanceId = 0;
// Set helpers for updates // Set helpers for updates
Globals.bLogHistoryUpdate = false; Globals.bLogHistoryUpdate = false;
Globals.bdcConnection = false; Globals.bdcConnection = false;

View File

@@ -124,6 +124,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="01_Classes\LogController.cs" />
<Compile Include="01_Classes\TCPController.cs" /> <Compile Include="01_Classes\TCPController.cs" />
<Compile Include="01_Classes\Globals.cs" /> <Compile Include="01_Classes\Globals.cs" />
<Compile Include="01_Classes\DatabaseController.cs" /> <Compile Include="01_Classes\DatabaseController.cs" />