diff --git a/JY.Inspection/Common/Log4Helper.cs b/JY.Inspection/Common/Log4Helper.cs index 63374fd..91e6f4d 100644 --- a/JY.Inspection/Common/Log4Helper.cs +++ b/JY.Inspection/Common/Log4Helper.cs @@ -5,6 +5,9 @@ namespace JY.Inspection { internal class Log4Helper { + // 通过名称获取专属的 PLC Logger + private static readonly ILog plcLog = LogManager.GetLogger("PlcDataLogger"); + public static void WriteLog(Type t, Exception ex) { ILog log = LogManager.GetLogger(t); diff --git a/JY.Inspection/Frm/FrmTest.Designer.cs b/JY.Inspection/Frm/FrmTest.Designer.cs index 0a68fce..b3b96ff 100644 --- a/JY.Inspection/Frm/FrmTest.Designer.cs +++ b/JY.Inspection/Frm/FrmTest.Designer.cs @@ -32,6 +32,7 @@ this.btn_stationExit = new MetroFramework.Controls.MetroButton(); this.btn_stationArrival = new MetroFramework.Controls.MetroButton(); this.btn_sendMQTTClient = new MetroFramework.Controls.MetroButton(); + this.btn_logPLCData = new MetroFramework.Controls.MetroButton(); this.SuspendLayout(); // // btn_readCollectItemCfg @@ -74,11 +75,22 @@ this.btn_sendMQTTClient.UseSelectable = true; this.btn_sendMQTTClient.Click += new System.EventHandler(this.btn_sendMQTTClient_Click); // + // btn_logPLCData + // + this.btn_logPLCData.Location = new System.Drawing.Point(263, 220); + this.btn_logPLCData.Name = "btn_logPLCData"; + this.btn_logPLCData.Size = new System.Drawing.Size(125, 53); + this.btn_logPLCData.TabIndex = 4; + this.btn_logPLCData.Text = "记录PLC数据"; + this.btn_logPLCData.UseSelectable = true; + this.btn_logPLCData.Click += new System.EventHandler(this.btn_logPLCData_Click); + // // FrmTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(998, 586); + this.Controls.Add(this.btn_logPLCData); this.Controls.Add(this.btn_sendMQTTClient); this.Controls.Add(this.btn_stationArrival); this.Controls.Add(this.btn_stationExit); @@ -95,5 +107,6 @@ private MetroFramework.Controls.MetroButton btn_stationExit; private MetroFramework.Controls.MetroButton btn_stationArrival; private MetroFramework.Controls.MetroButton btn_sendMQTTClient; + private MetroFramework.Controls.MetroButton btn_logPLCData; } } \ No newline at end of file diff --git a/JY.Inspection/Frm/FrmTest.cs b/JY.Inspection/Frm/FrmTest.cs index 0579e31..873e3a4 100644 --- a/JY.Inspection/Frm/FrmTest.cs +++ b/JY.Inspection/Frm/FrmTest.cs @@ -65,5 +65,14 @@ namespace JY.Inspection.Frm string payload = "测试数据"; await MqttSingleton.Instance.SendAsync(topic, payload); } + + private void btn_logPLCData_Click(object sender, EventArgs e) + { + byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; + + LogHelper.LogPLCData("电池出站数据", bytes); + + LogHelper.WriteErrorLine("错误日志"); + } } } diff --git a/JY.Inspection/HomeForm.cs b/JY.Inspection/HomeForm.cs index b1abee7..332678f 100644 --- a/JY.Inspection/HomeForm.cs +++ b/JY.Inspection/HomeForm.cs @@ -2283,6 +2283,7 @@ namespace JY.Inspection private void ParsePlcOutgoingData(byte[] content, OutgoingContainers containers) { var bytes = content.ByteReverse(); + JY.Utility.LogHelper.LogPLCData("电池出站数据", bytes); for (int i = 0; i < 2; i++) { diff --git a/JY.Inspection/HomeForm.resx b/JY.Inspection/HomeForm.resx index 47bb64a..2304c0d 100644 --- a/JY.Inspection/HomeForm.resx +++ b/JY.Inspection/HomeForm.resx @@ -331,7 +331,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAE - CgAAAk1TRnQBSQFMAgEBAwEAAcgBCAHIAQgBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CgAAAk1TRnQBSQFMAgEBAwEAAeABCAHgAQgBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA diff --git a/JY.Inspection/Log4net.config b/JY.Inspection/Log4net.config index f127236..7535d98 100644 --- a/JY.Inspection/Log4net.config +++ b/JY.Inspection/Log4net.config @@ -13,11 +13,30 @@ + + + + + + + + + + + + + + + + + + + diff --git a/JY.Utility/LogHelper.cs b/JY.Utility/LogHelper.cs index 5613fcb..8b6e565 100644 --- a/JY.Utility/LogHelper.cs +++ b/JY.Utility/LogHelper.cs @@ -18,6 +18,8 @@ namespace JY.Utility { private static readonly ILog log; + private static readonly ILog plcLog; + /// /// public static bool DebugMode = false; @@ -46,6 +48,8 @@ namespace JY.Utility Console.WriteLine("Log4net.config 文件未找到,日志将无法正常写入"); } log = LogManager.GetLogger(repository.Name, "Test"); + + plcLog = LogManager.GetLogger(repository.Name, "PlcDataLogger"); RunClearJob(); } @@ -372,6 +376,17 @@ namespace JY.Utility #endregion + public static void LogPLCData(string name, byte[] data) + { + if (data == null || data.Length == 0) + { + return; + } + + string hexString = BitConverter.ToString(data).Replace("-", " "); + + plcLog.Info($"{name} : {hexString}"); + } /// /// 错误日志带异常