diff --git a/JinYuan.ControlCenter/ControlCenter.cs b/JinYuan.ControlCenter/ControlCenter.cs index 30044a9..5d0b165 100644 --- a/JinYuan.ControlCenter/ControlCenter.cs +++ b/JinYuan.ControlCenter/ControlCenter.cs @@ -31,6 +31,7 @@ namespace JinYuan.ControlCenters { public partial class ControlCenter { + private static LoggerHelp _logger = new LoggerHelp(); /// /// 打印日志方法 /// @@ -139,7 +140,7 @@ namespace JinYuan.ControlCenters DicStationFunction = new Dictionary(); DicStationFunction.Add("电芯进站", FeedingStation); - //DicStationFunction.Add("NG出站", NGStation); + DicStationFunction.Add("NG出站", NGStation); DicStationFunction.Add("电芯出站", BlankingStation); for (int i = 0; i < CommonMethods.ChatLineCount; i++) @@ -262,14 +263,14 @@ namespace JinYuan.ControlCenters JYResult resByte = CommonMethods.PlcLink.ReadValue>($"{pf.VarList[1].VarAddress}", Data_Type.ArrByte, Convert.ToUInt16(pf.VarList[1].OffsetOrLenght)); sw1.Stop(); CommonMethods.AddDataMonitorLog(0, $"{pf.GroupName},耗时:{sw1.Elapsed.TotalMilliseconds}ms"); - LogHelper.Instance.WriteLog( $"读取工位:{pf.GroupName},启动读取字节共耗时:" + sw1.Elapsed.TotalMilliseconds, "循环读取PLC工位"); + LoggerHelp.WriteLog("循环读取PLC工位" + $"读取工位:{pf.GroupName},启动读取字节共耗时:" + sw1.Elapsed.TotalMilliseconds); //置位 PLC-防止重复读取 CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 2);//置位,读取进行中的标志 if (resByte == null) { CommonMethods.AddDataMonitorLog(0, $"上料工位:{pf.GroupName},起始地址:{pf.VarList[1].VarAddress},得到空字节数组"); - LogHelper.Instance.WriteLog( $"读取工位:{pf.GroupName},起始地址:{pf.VarList[1].VarAddress},得到空字节数组", "循环读取PLC上料工位"); + LoggerHelp.WriteLog("循环读取PLC上料工位"+$"读取工位:{pf.GroupName},起始地址:{pf.VarList[1].VarAddress},得到空字节数组"); } else { @@ -286,14 +287,14 @@ namespace JinYuan.ControlCenters // }, null); //} //// private Dictionary DicStationFunction; - DicStationFunction[pf.GroupName].BeginInvoke(resByte, pf, null, null);//信息解析 + DicStationFunction[pf.GroupName].BeginInvoke(resByte, pf, null, null);//信息解析 } } } catch (Exception ex) { CommonMethods.AddLog(true, ex.Message); - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } Thread.Sleep(200); @@ -1359,7 +1360,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } @@ -1842,7 +1843,7 @@ namespace JinYuan.ControlCenters catch (Exception ex) { CommonMethods.AddLog(true, "设备状态" + ex.Message); - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } #endregion diff --git a/JinYuan.ControlCenter/ControlCenter_Plc.cs b/JinYuan.ControlCenter/ControlCenter_Plc.cs index d5a4867..6eef0a7 100644 --- a/JinYuan.ControlCenter/ControlCenter_Plc.cs +++ b/JinYuan.ControlCenter/ControlCenter_Plc.cs @@ -204,11 +204,13 @@ namespace JinYuan.ControlCenters if (bArrays == null) { CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0 - CommonMethods.AddLog(false, $"电芯进站PLC有触发信号,但无数据!"); - LogHelper.Instance.WriteLog($"电芯进站PLC有触发信号,但无数据!", "Info"); + CommonMethods.AddLog(false, $"电芯进站PLC有触发信号,但无数据!");//存系统日志 + LoggerHelp.WriteLog($"电芯进站PLC有触发信号,但无数据!", "Info"); } if (bArrays.IsSuccess && bArrays.Content != null) { + + sw2.Restart(); //解析数据 //1257 3个条码 @@ -389,7 +391,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteError($"外观检测进站线程数据解析:{ex}", "SysErrorLog"); + LoggerHelp.WriteLog($"外观检测进站线程数据解析:{ex}", "SysErrorLog"); } @@ -485,7 +487,7 @@ namespace JinYuan.ControlCenters { JYResult result = CommonMethods.PlcLink.WriteValue("D80", 1, Data_Type.Short);//反馈PLC结果 CommonMethods.AddDataMonitorLog(1, $"服务器连接失败!解决办法:1、请检查MES网络,2、查看服务器是否正常!!"); - LogHelper.Instance.WriteError($"服务器连接失败:{ex}", "系统报错"); + LoggerHelp.WriteLog($"服务器连接失败:{ex}", "系统报错"); } } else @@ -501,13 +503,13 @@ namespace JinYuan.ControlCenters } else { - LogHelper.Instance.WriteError($"电芯进站数据List为空", "SysErrorLog"); + LoggerHelp.WriteLog($"电芯进站数据List为空", "SysErrorLog"); } } catch (Exception ex) { - LogHelper.Instance.WriteError($"外观检测上料保存处理异常:{ex}", "SysErrorLog"); + LoggerHelp.WriteLog($"外观检测上料保存处理异常:{ex}", "SysErrorLog"); } } #endregion @@ -522,7 +524,7 @@ namespace JinYuan.ControlCenters { CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0 CommonMethods.AddLog(false, $"NG出站PLC有触发信号,但无数据!"); - LogHelper.Instance.WriteLog($"NG出站PLC有触发信号,但无数据!", "SysErrorLog"); + LoggerHelp.WriteLog($"NG出站PLC有触发信号,但无数据!", "SysErrorLog"); } #region 预焊机 //if (bArrays.IsSuccess && bArrays.Content != null) @@ -1637,7 +1639,7 @@ namespace JinYuan.ControlCenters { CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0 CommonMethods.AddLog(false, $"电芯出站PLC有触发信号,但无数据!"); - LogHelper.Instance.WriteLog($"电芯出站PLC有触发信号,但无数据!", "SysErrorLog"); + LoggerHelp.WriteLog($"电芯出站PLC有触发信号,但无数据!", "SysErrorLog"); } if (bArrays.IsSuccess && bArrays.Content != null) { @@ -1911,7 +1913,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteError($"外观检测出站线程数据解析:{ex}", "SysErrorLog"); + LoggerHelp.WriteLog($"外观检测出站线程数据解析:{ex}", "SysErrorLog"); } } @@ -1942,7 +1944,7 @@ namespace JinYuan.ControlCenters } JYResult result3 = CommonMethods.PlcLink.WriteValue(pf.VarList[0].VarAddress, 0);//置位0 CommonMethods.AddDataMonitorLog(0, $"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]},{MyRes[2]}"); - LogHelper.Instance.WriteLog($"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]},{MyRes[2]}"); + LoggerHelp.WriteLog($"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]},{MyRes[2]}"); Task.Run(() => { SendDateMQTT.Instance.EnterandExitSendMessge(1); }); //数据存储 - 异步处理 @@ -2034,12 +2036,12 @@ namespace JinYuan.ControlCenters } else { - LogHelper.Instance.WriteError($"电芯出站数据List为空", "SysErrorLog"); + LoggerHelp.WriteLog($"电芯出站数据List为空", "SysErrorLog"); } } catch (Exception ex) { - LogHelper.Instance.WriteError($"电芯出站数据保存异常:{ex}", "SysErrorLog"); + LoggerHelp.WriteLog($"电芯出站数据保存异常:{ex}", "SysErrorLog"); } } #endregion diff --git a/JinYuan.ControlCenter/ControlCenter_Total.cs b/JinYuan.ControlCenter/ControlCenter_Total.cs index 072ce52..3556627 100644 --- a/JinYuan.ControlCenter/ControlCenter_Total.cs +++ b/JinYuan.ControlCenter/ControlCenter_Total.cs @@ -225,7 +225,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } }); @@ -234,7 +234,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } #endregion diff --git a/JinYuan.ControlCenter/MES/MESDataCombin.cs b/JinYuan.ControlCenter/MES/MESDataCombin.cs index 2b8c5a7..6a1826e 100644 --- a/JinYuan.ControlCenter/MES/MESDataCombin.cs +++ b/JinYuan.ControlCenter/MES/MESDataCombin.cs @@ -62,7 +62,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteEX("调用MES设备报警接口", ex); + LoggerHelp.WriteEX("调用MES设备报警接口", ex); } return resJson; @@ -104,7 +104,7 @@ namespace JinYuan.ControlCenters } catch (Exception ex) { - LogHelper.Instance.WriteEX("调用MES设备运行状态接口", ex); + LoggerHelp.WriteEX("调用MES设备运行状态接口", ex); } return resJson; } diff --git a/JinYuan.DAL/JinYuan.DAL.csproj b/JinYuan.DAL/JinYuan.DAL.csproj index 387461f..68671f1 100644 --- a/JinYuan.DAL/JinYuan.DAL.csproj +++ b/JinYuan.DAL/JinYuan.DAL.csproj @@ -53,6 +53,12 @@ ..\packages\MySql.Data.8.1.0\lib\net462\MySql.Data.dll + + ..\packages\NLog.6.0.2\lib\net46\NLog.dll + + + ..\packages\NLog.Windows.Forms.6.0.2\lib\net35\NLog.Windows.Forms.dll + ..\packages\SqlSugar.5.1.4.94\lib\SqlSugar.dll @@ -69,6 +75,7 @@ ..\packages\System.Diagnostics.DiagnosticSource.7.0.2\lib\net462\System.Diagnostics.DiagnosticSource.dll + ..\packages\System.IO.Pipelines.7.0.0\lib\net462\System.IO.Pipelines.dll @@ -99,6 +106,7 @@ ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + diff --git a/JinYuan.DAL/SQLSugarHelper.cs b/JinYuan.DAL/SQLSugarHelper.cs index 8879db1..26f1552 100644 --- a/JinYuan.DAL/SQLSugarHelper.cs +++ b/JinYuan.DAL/SQLSugarHelper.cs @@ -17,7 +17,7 @@ namespace JinYuan.DAL /// 数据库连接字符串 /// public static string connString = string.Empty; - + private static LoggerHelp _logger = new LoggerHelp(); /// /// 数据库类型 /// @@ -39,7 +39,7 @@ namespace JinYuan.DAL db.Ado.CommandTimeOut = 3000;//设置执行语法超时时间 db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完事件 { - LogHelper.Instance.WriteLog($"执行时间:{db.Ado.SqlExecutionTime.TotalMilliseconds}毫秒 \r\nSQL如下:{sql} \r\n参数:{GetParams(pars)} ", "SQL执行"); + LoggerHelp.WriteLog($"执行时间:{db.Ado.SqlExecutionTime.TotalMilliseconds}毫秒 \r\nSQL如下:{sql} \r\n参数:{GetParams(pars)} ", "SQL执行"); }; db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件 { @@ -47,7 +47,7 @@ namespace JinYuan.DAL }; db.Aop.OnError = (exp) =>//执行SQL 错误事件 { - LogHelper.Instance.WriteError($"SQL错误:{exp.Message}\r\nSQL如下:{exp.Sql}", "SQL执行"); + LoggerHelp.WriteLog($"SQL错误:{exp.Message}\r\nSQL如下:{exp.Sql}", "SQL执行"); throw new Exception(exp.Message); }; db.Aop.OnDiffLogEvent = (it) => //可以方便拿到 数据库操作前和操作后的数据变化。 diff --git a/JinYuan.DAL/SQLSugarService.cs b/JinYuan.DAL/SQLSugarService.cs index aa43c12..410c1cf 100644 --- a/JinYuan.DAL/SQLSugarService.cs +++ b/JinYuan.DAL/SQLSugarService.cs @@ -1,6 +1,7 @@ using JinYuan.DAL; using JinYuan.DAL.Enums; using JinYuan.DAL.Models; +using NLog; using Org.BouncyCastle.Asn1; using SqlSugar; using System; @@ -18,6 +19,8 @@ namespace JinYuan.DAL public class SQLSugarService : ISugarHepler { + + public void SetConnectionStr(string connectionStr, SqlSugar.DbType dbType= SqlSugar.DbType.SqlServer) { SQLSugarHelper.connString = connectionStr; @@ -856,7 +859,7 @@ namespace JinYuan.DAL } catch (Exception ex) { - + LoggerHelp.WriteInfo("aaa"); throw new Exception(ex.Message); } diff --git a/JinYuan.DAL/packages.config b/JinYuan.DAL/packages.config index c363f2e..b5cb6cd 100644 --- a/JinYuan.DAL/packages.config +++ b/JinYuan.DAL/packages.config @@ -6,6 +6,8 @@ + + diff --git a/JinYuan.Helper/DeleteLog.cs b/JinYuan.Helper/DeleteLog.cs index e9f0592..8d65c9c 100644 --- a/JinYuan.Helper/DeleteLog.cs +++ b/JinYuan.Helper/DeleteLog.cs @@ -26,7 +26,7 @@ namespace JinYuan.Helper { bool b = DeleteFile(strPata, 30); //删除该目录下 超过 30天的文件 if (b) - LogHelper.Instance.WriteLog("已清除30天内过期日志"); + LoggerHelp.WriteLog("已清除30天内过期日志"); //24小时清一次 Thread.Sleep(1000 * 60 * 60 * 24); } @@ -75,14 +75,14 @@ namespace JinYuan.Helper } else { - LogHelper.Instance.WriteLog("文件被占用,无法操作!"); + LoggerHelp.WriteLog("文件被占用,无法操作!"); } } } } catch (Exception err) { - LogHelper.Instance.WriteLog($"文件被占用,无法操作!{err}"); + LoggerHelp.WriteLog($"文件被占用,无法操作!{err}"); } return false; } diff --git a/JinYuan.Helper/JinYuan.Helper.csproj b/JinYuan.Helper/JinYuan.Helper.csproj index 8b96cee..f327042 100644 --- a/JinYuan.Helper/JinYuan.Helper.csproj +++ b/JinYuan.Helper/JinYuan.Helper.csproj @@ -47,9 +47,6 @@ ..\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll - - ..\LargeSquareOne\bin\Debug\log4net.dll - ..\packages\MathNet.Numerics.Signed.4.15.0\lib\net461\MathNet.Numerics.dll @@ -65,6 +62,9 @@ ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\NLog.6.0.2\lib\net46\NLog.dll + ..\packages\NPOI.2.6.1\lib\netstandard2.0\NPOI.Core.dll @@ -151,10 +151,9 @@ - + - diff --git a/JinYuan.Helper/LogHelper.cs b/JinYuan.Helper/LogHelper.cs deleted file mode 100644 index 748f072..0000000 --- a/JinYuan.Helper/LogHelper.cs +++ /dev/null @@ -1,188 +0,0 @@ -using JinYuan.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JinYuan.Helper -{ - /// - /// 日志操作类 - /// - public class LogHelper - { - private static object singleLock = new object(); - - /// - /// log日志文件路径 - /// - public static string logFilePath = string.Empty; - - - public static LogHelper _instance = null; - public static LogHelper Instance - { - - get - { - if (_instance == null) - { - lock (singleLock) - { - if (_instance == null) - { - _instance = new LogHelper(); - } - } - } - return _instance; - } - - } - - - - - /// - /// 初始化 - /// - /// - public void InitLog(string configPath) - { - logFilePath = configPath; - if (!Directory.Exists(logFilePath)) - { - Directory.CreateDirectory(logFilePath); - } - //if (!File.Exists(logFilePath)) - //{ - // File.Create(logFilePath).Close(); - //} - } - - - /// - /// 写日志文件数据库日志文件 - /// - /// 消息 - public void WriteError(string message) - { - - AddLog(message, "Error"); - } - - public void WriteEX(Exception ex) - { - AddLog(ex.Message + "\r\n" + ex.InnerException + "\r\n" + ex.StackTrace + "\r\n" + ex.Source, "SysErrorLog"); - } - - public void WriteEX(string ErrorName,Exception ex) - { - AddLog(ErrorName+ "\r\n"+ex.Message + "\r\n" + ex.InnerException + "\r\n" + ex.StackTrace + "\r\n" + ex.Source, "SysErrorLog"); - } - /// - /// 写日志文件数据库日志文件 - /// - /// 消息 - /// 日志存储目录名称 - public void WriteError(Exception ex, string direName) - { - AddLog(ex.Message + "\r\n" + ex.InnerException + "\r\n" + ex.StackTrace + "\r\n" + ex.Source, direName); - } - - /// - /// / - /// - /// - /// - public void WriteError(string message, string direName) - { - AddLog(message, direName); - } - /// - /// 写日志文件数据库日志文件 - /// - /// 消息 - public void WriteLog(string message) - { - - AddLog(message, "Info"); - } - /// - /// 写日志文件数据库日志文件 - /// - /// 消息 - /// 日志存储目录名称 - public void WriteLog(string message, string direName) - { - - AddLog(message, direName); - } - - /// - /// 写日志文件数据库日志文件 - /// - /// 消息 - /// 日志存储目录名称 - private void AddLog(string message, string direName) - { - string fileLog = logFilePath; - if (fileLog == "") - { - return; - } - try - { - var applicationName = direName; - //从宿主配置文件中获取日志文件全路径 - //所有的接口日志文件放在一个目录下面,跟web站点的目录分开 - //日志文件一天放一个,按日期分开 - - if (string.IsNullOrEmpty(applicationName)) - { - applicationName = "Log"; - } - string logFullPath = fileLog + applicationName; - - if (!Directory.Exists(logFullPath)) - { - Directory.CreateDirectory(logFullPath); - } - //只保留30天的日志 - var deletePath = $@"{logFullPath}\{DateTime.Now.AddDays(-30):yyyyMMddHH}.txt"; - - if (File.Exists(deletePath)) - { - File.Delete(deletePath); - } - - logFullPath = $@"{logFullPath}\{DateTime.Now:yyyyMMddHH}.txt"; - if (!File.Exists(logFullPath)) - { - using (var fs = new FileStream(logFullPath, FileMode.Create, FileAccess.Write)) - { - StreamWriter sw = new StreamWriter(fs); - sw.Close(); - fs.Close(); - } - } - - using (StreamWriter writer = new StreamWriter(logFullPath, true)) - { - writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - writer.WriteLine(message); - writer.WriteLine(Environment.NewLine); - - } - - } - catch - { - // ignored - } - } - - } -} diff --git a/JinYuan.Helper/Logger.cs b/JinYuan.Helper/Logger.cs deleted file mode 100644 index 9c7a004..0000000 --- a/JinYuan.Helper/Logger.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - - -[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)] -public class Logger -{ - private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); - private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); - private static readonly log4net.ILog logmqtt = log4net.LogManager.GetLogger("logmqtt"); - - public static void WriteInfo(string info) - { - Console.WriteLine(info); - if (loginfo.IsInfoEnabled) - { - loginfo.Info(info); - } - } - - public static void WriteMqtt(string info) - { - Console.WriteLine(info); - if (logmqtt.IsInfoEnabled) - { - logmqtt.Info(info); - } - } - - public static void WriteError(string error) - { - Console.WriteLine(error); - if (logerror.IsErrorEnabled) - { - logerror.Error(error); - } - } - - public static void WriteError(string info, Exception ex) - { - Console.WriteLine(info); - if (logerror.IsErrorEnabled) - { - logerror.Error(info, ex); - } - } -} - diff --git a/JinYuan.Helper/LoggerHelp.cs b/JinYuan.Helper/LoggerHelp.cs new file mode 100644 index 0000000..fe87a20 --- /dev/null +++ b/JinYuan.Helper/LoggerHelp.cs @@ -0,0 +1,75 @@ +using NLog; +using System; +using System.IO; +using System.Linq; + +public class LoggerHelp +{ + private static readonly NLog.Logger loginfo = LogManager.GetLogger("loginfo"); + private static readonly NLog.Logger logerror = LogManager.GetLogger("logerror"); + private static readonly NLog.Logger logmqtt = LogManager.GetLogger("logmqtt"); + + private static readonly NLog.Logger logsInfo = LogManager.GetLogger("logsInfo"); + private static readonly NLog.Logger logsMEStIME = LogManager.GetLogger("logsMEStIME"); + private static readonly NLog.Logger logsSql = LogManager.GetLogger("logsSql"); + private static readonly NLog.Logger logsSysErrorLog = LogManager.GetLogger("logsSysErrorLog"); + private static readonly NLog.Logger logsSysErrorLogcn = LogManager.GetLogger("logsSysErrorLogcn"); + + + public static void WriteInfo(string info) => loginfo.Info(info); + + + public static void WriteMqtt(string info) => logmqtt.Info(info); + + public static void WriteError(string error) => logerror.Info(error); + + public static void WriteError(string info, Exception ex) => logerror.Error(info, ex); + + + public static void WriteEX(Exception ex) => logsSysErrorLog.Error(ex.Message + "\r\n" + ex.InnerException + "\r\n" + ex.StackTrace + "\r\n" + ex.Source, "SysErrorLog"); + + public static void WriteEX(string ErrorName, Exception ex) + { + logsSysErrorLog.Error(ErrorName + "\r\n" + ex.Message + "\r\n" + ex.InnerException + "\r\n" + ex.StackTrace + "\r\n" + ex.Source, "SysErrorLog"); + } + + /// + /// 写日志文件数据库日志文件 + /// + /// 消息 + public static void WriteLog(string message) + { + // logsInfo.IsInfoEnabled = true; + logsInfo.Info(message); + } + + /// + /// 写日志文件数据库日志文件 + /// + /// 消息 + /// 日志存储目录名称 + public static void WriteLog(string message, string direName) + { + switch (direName) + { + case "Info": + logsInfo.Info(message, "Info"); + break; + case "SysErrorLog": + logsSysErrorLog.Info(message, "SysErrorLog"); + break; + case "SQL执行": + logsSql.Info(message); + break; + case "MESTime": + logsMEStIME.Info(message, "MESTime"); + break; + case "系统报错": + logsSysErrorLogcn.Info(message, "系统报错"); + break; + default: + break; + } + } +} + diff --git a/JinYuan.Helper/SendDateMQTT.cs b/JinYuan.Helper/SendDateMQTT.cs index 343fb7a..0719b03 100644 --- a/JinYuan.Helper/SendDateMQTT.cs +++ b/JinYuan.Helper/SendDateMQTT.cs @@ -121,7 +121,7 @@ namespace JinYuan.Helper } catch (Exception err) { - Logger.WriteError($"发送进出站数据错误,{err.Message}",err); + LoggerHelp.WriteError($"发送进出站数据错误,{err.Message}",err); } } /// @@ -144,7 +144,7 @@ namespace JinYuan.Helper } catch (Exception err) { - Logger.WriteError($"发送采集项数据错误,{err.Message}",err); + LoggerHelp.WriteError($"发送采集项数据错误,{err.Message}",err); } } @@ -162,7 +162,7 @@ namespace JinYuan.Helper { if (mqttClient != null) { - Logger.WriteMqtt("MQTT 服务器关闭失败"); + LoggerHelp.WriteMqtt("MQTT 服务器关闭失败"); mqttClient.ConnectClose(); } @@ -177,16 +177,16 @@ namespace JinYuan.Helper OperateResult connect = mqttClient.ConnectServer(); if (connect.IsSuccess) { - Logger.WriteMqtt("MQTT 连接服务器成功"); + LoggerHelp.WriteMqtt("MQTT 连接服务器成功"); } else { - Logger.WriteMqtt("MQTT 无法连接到服务器"); + LoggerHelp.WriteMqtt("MQTT 无法连接到服务器"); } } catch (Exception err) { - Logger.WriteError($"MQTT 无法连接到服务器: {err.Message}"); + LoggerHelp.WriteError($"MQTT 无法连接到服务器: {err.Message}"); } @@ -209,16 +209,16 @@ namespace JinYuan.Helper }); if (result.IsSuccess) { - Logger.WriteMqtt($"发送消息到主题 [{topic}]: {Message}"); + LoggerHelp.WriteMqtt($"发送消息到主题 [{topic}]: {Message}"); } else { - Logger.WriteMqtt($"发送消息到主题 [{topic}] 失败: {result.Message}"); + LoggerHelp.WriteMqtt($"发送消息到主题 [{topic}] 失败: {result.Message}"); } } catch (Exception err) { - Logger.WriteError($"MQTT 无法连接到服务器: {err.Message}"); + LoggerHelp.WriteError($"MQTT 无法连接到服务器: {err.Message}"); } } @@ -234,22 +234,22 @@ namespace JinYuan.Helper OperateResult Result = mqttClient.SubscribeMessage(Topic); // 订阅A的主题 if (Result.IsSuccess) { - Logger.WriteMqtt($"订阅成功[{Topic}]"); + LoggerHelp.WriteMqtt($"订阅成功[{Topic}]"); } else { - Logger.WriteMqtt($"订阅失败[{Topic}]"); + LoggerHelp.WriteMqtt($"订阅失败[{Topic}]"); } } catch (Exception err) { - Logger.WriteError($"订阅失败[{err.Message}]"); + LoggerHelp.WriteError($"订阅失败[{err.Message}]"); } } private static void MqttClient_OnMqttMessageReceived(MqttClient client, MqttApplicationMessage message) { - Logger.WriteMqtt($"收到服务器信息[{message.ToString()}]"); + LoggerHelp.WriteMqtt($"收到服务器信息[{message.ToString()}]"); } /// @@ -284,7 +284,7 @@ namespace JinYuan.Helper } catch (Exception ex) { - Logger.WriteError($"方法:上传MQTTServer数据,上传失败: {ex.Message}", ex); + LoggerHelp.WriteError($"方法:上传MQTTServer数据,上传失败: {ex.Message}", ex); } return messgeJson; } @@ -351,7 +351,7 @@ namespace JinYuan.Helper } catch (Exception ex) { - Logger.WriteError($"生成Json数据失败: {ex.Message}", ex); + LoggerHelp.WriteError($"生成Json数据失败: {ex.Message}", ex); } return messgejson; } diff --git a/JinYuan.Helper/packages.config b/JinYuan.Helper/packages.config index db731ea..4d2b9ec 100644 --- a/JinYuan.Helper/packages.config +++ b/JinYuan.Helper/packages.config @@ -9,6 +9,7 @@ + diff --git a/JinYuan.MES/MesDataProcess.cs b/JinYuan.MES/MesDataProcess.cs index 53d3c64..6663edb 100644 --- a/JinYuan.MES/MesDataProcess.cs +++ b/JinYuan.MES/MesDataProcess.cs @@ -17,6 +17,8 @@ namespace JinYuan.MES { public class MesDataProcess : IMes { + private static LoggerHelp _logger = new LoggerHelp(); + #region 1、登录验证 /// /// 登录MES验证 @@ -80,7 +82,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"调用员工权限校验接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("调用员工权限校验接口", ex); + LoggerHelp.WriteEX("调用员工权限校验接口", ex); } return res; @@ -145,8 +147,9 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"设备参数设定值请求接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("设备参数设定值请求接口", ex); + LoggerHelp.WriteEX("设备参数设定值请求接口", ex); } + return null; } @@ -207,7 +210,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"参数设定值变更接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("参数设定值变更接口", ex); + LoggerHelp.WriteEX("参数设定值变更接口", ex); } return res; @@ -262,7 +265,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"预焊进站PC<=>MES上传耗时:{outTime}ms,电芯码:{BarCode}", "MESTime"); + LoggerHelp.WriteLog($"预焊进站PC<=>MES上传耗时:{outTime}ms,电芯码:{BarCode}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -296,7 +299,7 @@ namespace JinYuan.MES Mesage = "调用进站接口超时或MES无返回信息!"; mesResType = MesResType.B; resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); + LoggerHelp.WriteEX("产品进站信息(组装)接口", ex); } return res; @@ -353,7 +356,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"钢壳进站PC<=>MES上传耗时:{outTime}ms,钢壳码:{BarCode}", "MESTime"); + LoggerHelp.WriteLog($"钢壳进站PC<=>MES上传耗时:{outTime}ms,钢壳码:{BarCode}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -383,7 +386,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); + LoggerHelp.WriteEX("产品进站信息(组装)接口", ex); } return res; @@ -440,7 +443,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"预焊进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime"); + LoggerHelp.WriteLog($"预焊进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -475,7 +478,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); + LoggerHelp.WriteEX("产品进站信息(组装)接口", ex); } return res; @@ -555,7 +558,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"钢壳进出站PC<=>MES上传耗时:{outTime}ms,钢壳码:{m.CSBYHCODEA}", "MESTime"); + LoggerHelp.WriteLog($"钢壳进出站PC<=>MES上传耗时:{outTime}ms,钢壳码:{m.CSBYHCODEA}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -584,7 +587,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"产品出站信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品出站信息(组装)接口", ex); + LoggerHelp.WriteEX("产品出站信息(组装)接口", ex); } return res; @@ -665,7 +668,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"钢壳进出站PC<=>MES上传耗时:{outTime}ms,钢壳码:{m.CSBYHCODEA}", "MESTime"); + LoggerHelp.WriteLog($"钢壳进出站PC<=>MES上传耗时:{outTime}ms,钢壳码:{m.CSBYHCODEA}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -694,7 +697,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"产品进出站一体信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品进出站一体信息(组装)接口", ex); + LoggerHelp.WriteEX("产品进出站一体信息(组装)接口", ex); } return res; @@ -785,7 +788,7 @@ namespace JinYuan.MES long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { - LogHelper.Instance.WriteLog($"外观检出站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar.CSBYHCODEA}", "MESTime"); + LoggerHelp.WriteLog($"预焊出站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar.CSBYHCODEA}", "MESTime"); } if (string.IsNullOrEmpty(result)) @@ -816,7 +819,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"产品出站信息(组装)接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("产品出站信息(组装)接口", ex); + LoggerHelp.WriteEX("产品出站信息(组装)接口", ex); } return res; @@ -2864,7 +2867,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"设备运行状态接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("设备运行状态接口", ex); + LoggerHelp.WriteEX("设备运行状态接口", ex); } return res; @@ -2933,7 +2936,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"调用设备报警信息接口发生异常:{ex.Message}"; - LogHelper.Instance.WriteEX("调用设备报警信息接口", ex); + LoggerHelp.WriteEX("调用设备报警信息接口", ex); } return res; @@ -2976,7 +2979,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"调用设备能耗信息接口:{ex.Message}"; - LogHelper.Instance.WriteEX("调用设备能耗信息接口", ex); + LoggerHelp.WriteEX("调用设备能耗信息接口", ex); } return res; @@ -3028,7 +3031,7 @@ namespace JinYuan.MES catch (Exception ex) { resJson = $"调用风速接口:{ex.Message}"; - LogHelper.Instance.WriteEX("调用风速接口", ex); + LoggerHelp.WriteEX("调用风速接口", ex); } return res; diff --git a/LargeSquareOne/FrmAlarmLog.cs b/LargeSquareOne/FrmAlarmLog.cs index 0182802..df23765 100644 --- a/LargeSquareOne/FrmAlarmLog.cs +++ b/LargeSquareOne/FrmAlarmLog.cs @@ -1,6 +1,7 @@ using JinYuan.Helper; using JinYuan.Models; using JinYuan.VirtualDataLibrary; +using PLCCommunication; using System; using System.Windows.Forms; @@ -38,20 +39,21 @@ namespace LargeSquareOne this.dgv_Log.Rows[rowCount].Cells[2].Value = info; // 设置最新行为活动行 (当前行) this.dgv_Log.CurrentCell = this.dgv_Log.Rows[this.dgv_Log.Rows.Count - 1].Cells[0]; - - - } - - //存储到数据库 - CommonMethods.db.AddReturnBool(new SysLog() + var model = new SysLog() { InsertTime = CurrentTime, LogType = "系统日志", Note = info, AlarmType = isError ? "错误" : "信息", Operater = CommonMethods.currentAdmin.LoginName - }); + }; + //存储到数据库 + var b = CommonMethods.db.AddReturnBool(model); + if (!b) + { + LoggerHelp.WriteError($"数据库记录失败. 表:SysLog,内容:{model.ToJsonString()}"); + } } /// diff --git a/LargeSquareOne/FrmDataMonitor.cs b/LargeSquareOne/FrmDataMonitor.cs index 3fd51f5..b22d3cd 100644 --- a/LargeSquareOne/FrmDataMonitor.cs +++ b/LargeSquareOne/FrmDataMonitor.cs @@ -176,7 +176,7 @@ namespace LargeSquareOne catch (Exception ex) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } @@ -228,7 +228,7 @@ namespace LargeSquareOne catch (Exception ex) { - LogHelper.Instance.WriteEX("获取参数失败", ex); + LoggerHelp.WriteEX("获取参数失败", ex); } } @@ -331,7 +331,7 @@ namespace LargeSquareOne { strErr = "取消变更,采用本地参数配置"; } - LogHelper.Instance.WriteLog(strErr); + LoggerHelp.WriteLog(strErr); } } } @@ -341,7 +341,7 @@ namespace LargeSquareOne { strErr = "获取参数失败" + ex.Message; - LogHelper.Instance.WriteEX("获取参数失败", ex); + LoggerHelp.WriteEX("获取参数失败", ex); } return false; @@ -489,7 +489,7 @@ namespace LargeSquareOne return; } CommonMethods.AddDataMonitorLog(1, "MES参数设置成功"); - LogHelper.Instance.WriteLog("MES参数设置成功"); + LoggerHelp.WriteLog("MES参数设置成功"); } #endregion diff --git a/LargeSquareOne/FrmMain.cs b/LargeSquareOne/FrmMain.cs index 1e8f8ef..c99482f 100644 --- a/LargeSquareOne/FrmMain.cs +++ b/LargeSquareOne/FrmMain.cs @@ -362,7 +362,7 @@ namespace LargeSquareOne } catch (Exception ex) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); } } @@ -998,7 +998,7 @@ namespace LargeSquareOne private void LoginLocalOut() { CommonMethods.AddOPLog(false, $"退出成功,退出用户:{CommonMethods.currentAdmin?.LoginName},权限:{CommonMethods.currentAdmin?.LoginLevel}"); - LogHelper.Instance.WriteLog($"用户:" + CommonMethods.currentAdmin?.LoginName + "-退出"); + LoggerHelp.WriteLog($"用户:" + CommonMethods.currentAdmin?.LoginName + "-退出"); CommonMethods.LoginOut = true; } diff --git a/LargeSquareOne/FrmRecipe.cs b/LargeSquareOne/FrmRecipe.cs index c13dd59..d30f45a 100644 --- a/LargeSquareOne/FrmRecipe.cs +++ b/LargeSquareOne/FrmRecipe.cs @@ -221,12 +221,12 @@ namespace LargeSquareOne { if (ex.Message.Contains("重复键")) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); new FrmMsgBoxOutWithAck(3, "添加失败:已存在改型号,重复新增!", "添加型号").Show(); } else { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); new FrmMsgBoxOutWithAck(3, $"添加失败:{ex.Message}", "添加型号").Show(); } } @@ -402,12 +402,12 @@ namespace LargeSquareOne { if (ex.Message.Contains("重复键")) { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); new FrmMsgBoxOutWithAck(3, "添加失败:已存在该挑战件条码,重复新增!", "添加添加挑战件条码").Show(); } else { - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); new FrmMsgBoxOutWithAck(3, $"添加失败:{ex.Message}", "添加添加挑战件条码").Show(); } } diff --git a/LargeSquareOne/HslCommunication.dll b/LargeSquareOne/HslCommunication.dll deleted file mode 100644 index a9c8bf8..0000000 Binary files a/LargeSquareOne/HslCommunication.dll and /dev/null differ diff --git a/LargeSquareOne/LargeSquareOne.csproj b/LargeSquareOne/LargeSquareOne.csproj index 1c6f773..b9ee221 100644 --- a/LargeSquareOne/LargeSquareOne.csproj +++ b/LargeSquareOne/LargeSquareOne.csproj @@ -93,10 +93,6 @@ ..\packages\K4os.Hash.xxHash.1.0.8\lib\net462\K4os.Hash.xxHash.dll - - False - bin\Debug\log4net.dll - ..\packages\MathNet.Numerics.5.0.0\lib\net461\MathNet.Numerics.dll @@ -115,6 +111,12 @@ ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\NLog.6.0.2\lib\net46\NLog.dll + + + ..\packages\NLog.Windows.Forms.6.0.2\lib\net35\NLog.Windows.Forms.dll + @@ -417,6 +419,9 @@ + + Always + SettingsSingleFileGenerator @@ -455,6 +460,7 @@ + diff --git a/LargeSquareOne/NLog.config b/LargeSquareOne/NLog.config new file mode 100644 index 0000000..4ca60d8 --- /dev/null +++ b/LargeSquareOne/NLog.config @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LargeSquareOne/Program.cs b/LargeSquareOne/Program.cs index 88c8aa7..7ac66c8 100644 --- a/LargeSquareOne/Program.cs +++ b/LargeSquareOne/Program.cs @@ -2,6 +2,8 @@ using JinYuan.Helper; using JinYuan.Models; using JinYuan.VirtualDataLibrary; +using NLog; +using NLog.Config; using System; using System.Collections.Generic; using System.Configuration; @@ -36,6 +38,7 @@ namespace LargeSquareOne [STAThread] static void Main() { + LoggerHelp.WriteInfo("Application 启动"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string strname = Process.GetCurrentProcess().ProcessName; @@ -66,7 +69,7 @@ namespace LargeSquareOne { Application.Run(new FrmMain()); } - + } catch (Exception ex) { @@ -83,7 +86,6 @@ namespace LargeSquareOne public static void ReadConfig() { - LogHelper.Instance.InitLog(Application.StartupPath + "\\Logs\\"); IniConfigHelper.CreateIniFile(Application.StartupPath + "\\Config\\", "Configure.ini"); string connString = ConfigurationManager.ConnectionStrings["connString"].ToString(); string connString2 = ConfigurationManager.ConnectionStrings["connString2"].ToString(); @@ -91,7 +93,7 @@ namespace LargeSquareOne var connDbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), strDBType); CommonMethods.db.SetConnectionStr(connString, connDbType); CommonMethods.dbServer.SetConnectionStr(connString2, connDbType); - CommonMethods.sysConfig.SwipeCardMode=Convert.ToBoolean(IniConfigHelper.ReadIniData("配置信息", "开启刷卡模式", "false")); + CommonMethods.sysConfig.SwipeCardMode = Convert.ToBoolean(IniConfigHelper.ReadIniData("配置信息", "开启刷卡模式", "false")); } @@ -107,7 +109,7 @@ namespace LargeSquareOne { Exception ex = (Exception)e.ExceptionObject; - LogHelper.Instance.WriteEX(ex); + LoggerHelp.WriteEX(ex); MessageBox.Show($"程序出现大异常,请联系 【金源自动化软件设计部】。\r\n异常信息: {ex.Message}\r\n异常堆栈:{ex.StackTrace}", "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); } diff --git a/LargeSquareOne/TextFile1.txt b/LargeSquareOne/TextFile1.txt new file mode 100644 index 0000000..dabeed8 --- /dev/null +++ b/LargeSquareOne/TextFile1.txt @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LargeSquareOne/log4net.config b/LargeSquareOne/log4net.config deleted file mode 100644 index 21d5010..0000000 --- a/LargeSquareOne/log4net.config +++ /dev/null @@ -1,65 +0,0 @@ - - - -
- - - 错误日志类 - - - - - 信息日志类 - - - - - 信息日志类 - - - - - 错误日志附加介质 - - - - - - - - - 布局 - - - - - 信息日志附加介质 - - - - - - - - - 信息日志布局 - - - - - 信息日志附加介质 - - - - - - - - - 信息日志布局 - - - - - - \ No newline at end of file diff --git a/LargeSquareOne/log4net.dll b/LargeSquareOne/log4net.dll deleted file mode 100644 index edbdb32..0000000 Binary files a/LargeSquareOne/log4net.dll and /dev/null differ diff --git a/LargeSquareOne/packages.config b/LargeSquareOne/packages.config index 1d551c0..d1d60ab 100644 --- a/LargeSquareOne/packages.config +++ b/LargeSquareOne/packages.config @@ -12,6 +12,8 @@ + +