Browse Source

fixed eap log override bug;
After manual stopping FA when host is connecting equipment,then cannot reEnable FA

chenkui 4 months ago
parent
commit
5516ea78ad
2 changed files with 154 additions and 133 deletions
  1. 139 130
      SecsGem.Core/Log/Logger.cs
  2. 15 3
      SecsGem.Hsms.Core/NetServer.cs

+ 139 - 130
SecsGem.Core/Log/Logger.cs

@@ -1,130 +1,139 @@
-using Aitex.Core.RT.SCCore;
-using log4net.Appender;
-using log4net.Core;
-using log4net.Layout;
-using log4net.Repository.Hierarchy;
-using SecsGem.Core.EnumData;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace SecsGem.Core.Log
-{
-    public class Logger
-    {
-        private ILogger logger = null;
-        public Logger(string moduleName)
-        {
-            Hierarchy hierarchy = (Hierarchy)LoggerManager.CreateRepository(moduleName);
-            hierarchy.Name = moduleName;
-            RollingFileAppender roller = new RollingFileAppender();
-            roller.Name = "EapRoller";
-            roller.Layout = new PatternLayout();
-            roller.AppendToFile = true;
-            roller.RollingStyle = RollingFileAppender.RollingMode.Composite;
-            roller.MaximumFileSize = "100MB";
-            roller.StaticLogFileName = false;
-            if (SC.ContainsItem("System.LogFilePath"))
-            {
-                string logFilePath = SC.GetStringValue("System.LogFilePath");
-                if (!string.IsNullOrEmpty(logFilePath))
-                {
-                    roller.File = $"{logFilePath}\\EAP-{moduleName}";
-                }
-                else
-                {
-                    roller.File = @"Logs\EAP-" + $"{moduleName}";
-                }
-            }
-            else
-            {
-                roller.File = @"Logs\EAP-" + $"{moduleName}";
-            }
-
-            roller.DatePattern = "-yyyyMMdd'.log'";
-            roller.ActivateOptions();
-            hierarchy.Root.AddAppender(roller);
-
-            hierarchy.Root.Level = log4net.Core.Level.All;
-            hierarchy.Configured = true;
-            logger = log4net.Core.LoggerManager.GetLogger(moduleName, "EapRoller");
-        }
-
-        public void Info(string msg)
-        {
-            WriteLog(msg, Level.Info, LogFormat.System, null);
-        }
-
-        public void Debug(string msg)
-        {
-            WriteLog(msg, Level.Debug, LogFormat.System, null);
-        }
-
-        public void Warn(string msg)
-        {
-            WriteLog(msg, Level.Warn, LogFormat.System, null);
-        }
-
-        public void Error(string msg,Exception ex)
-        {
-            WriteLog(msg, Level.Error, LogFormat.System, ex);
-        }
-
-
-        public void WriteEquipmentInfo(string msg)
-        {
-            WriteLog(msg, Level.Info, LogFormat.EquipmentToHost, null);
-        }
-
-        public void WritHostInfo(string msg)
-        {
-            WriteLog(msg, Level.Info, LogFormat.HostToEquipment, null);
-        }
-
-        public void WriteBuffer(string msg)
-        {
-            WriteLog(msg, Level.Info, LogFormat.Buffer, null);
-        }
-
-        private void WriteLog(string msg,Level level,LogFormat logFormat,Exception ex)
-        {
-            try
-            {
-                logger.Log(typeof(Logger), level, FormatLogMessage(msg, level, logFormat), ex);
-            }
-            catch
-            {
-                
-            }
-        }
-        /// <summary>
-        /// 格式化
-        /// </summary>
-        /// <param name="msg"></param>
-        /// <param name="level"></param>
-        /// <param name="logFormat"></param>
-        /// <returns></returns>
-        private string FormatLogMessage(string msg,Level level,LogFormat logFormat)
-        {
-            string str = $"[{ level.Name}]";
-            switch(logFormat)
-            {
-                case LogFormat.HostToEquipment:
-                    str = "[H->E]";
-                    break;
-                case LogFormat.EquipmentToHost:
-                    str = "[E->H]";
-                    break;
-                case LogFormat.Buffer:
-                    str = "[BUF]";
-                    break;
-
-            }
-
-            return string.Format("{0}\t{1}\t{2} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
-                    str,msg);
-        }
-    }
-}
+using Aitex.Core.RT.SCCore;
+using log4net.Appender;
+using log4net.Core;
+using log4net.Layout;
+using log4net.Repository.Hierarchy;
+using SecsGem.Core.EnumData;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SecsGem.Core.Log
+{
+    public class Logger
+    {
+        private ILogger logger = null;
+        public Logger(string moduleName)
+        {
+            Hierarchy hierarchy = (Hierarchy)LoggerManager.CreateRepository(moduleName);
+            hierarchy.Name = moduleName;
+            RollingFileAppender roller = new RollingFileAppender();
+            roller.Name = "EapRoller";
+            roller.Layout = new PatternLayout();
+            roller.AppendToFile = true;
+            roller.RollingStyle = RollingFileAppender.RollingMode.Composite;
+            if (SC.ContainsItem("FA.MaximumFileSize"))
+            {
+                string maxFileSize = SC.GetStringValue("FA.MaximumFileSize");
+                roller.MaximumFileSize = maxFileSize;
+            }
+            else
+            {
+                roller.MaximumFileSize = "100MB";
+            }
+            roller.MaxSizeRollBackups = 50;
+            roller.StaticLogFileName = false;
+            if (SC.ContainsItem("System.LogFilePath"))
+            {
+                string logFilePath = SC.GetStringValue("System.LogFilePath");
+                if (!string.IsNullOrEmpty(logFilePath))
+                {
+                    roller.File = $"{logFilePath}\\EAP-{moduleName}";
+                }
+                else
+                {
+                    roller.File = @"Logs\EAP-" + $"{moduleName}";
+                }
+            }
+            else
+            {
+                roller.File = @"Logs\EAP-" + $"{moduleName}";
+            }
+
+            roller.DatePattern = "-yyyyMMdd'.log'";
+            roller.ActivateOptions();
+            hierarchy.Root.AddAppender(roller);
+
+            hierarchy.Root.Level = log4net.Core.Level.All;
+            hierarchy.Configured = true;
+            logger = log4net.Core.LoggerManager.GetLogger(moduleName, "EapRoller");
+        }
+
+        public void Info(string msg)
+        {
+            WriteLog(msg, Level.Info, LogFormat.System, null);
+        }
+
+        public void Debug(string msg)
+        {
+            WriteLog(msg, Level.Debug, LogFormat.System, null);
+        }
+
+        public void Warn(string msg)
+        {
+            WriteLog(msg, Level.Warn, LogFormat.System, null);
+        }
+
+        public void Error(string msg,Exception ex)
+        {
+            WriteLog(msg, Level.Error, LogFormat.System, ex);
+        }
+
+
+        public void WriteEquipmentInfo(string msg)
+        {
+            WriteLog(msg, Level.Info, LogFormat.EquipmentToHost, null);
+        }
+
+        public void WritHostInfo(string msg)
+        {
+            WriteLog(msg, Level.Info, LogFormat.HostToEquipment, null);
+        }
+
+        public void WriteBuffer(string msg)
+        {
+            WriteLog(msg, Level.Info, LogFormat.Buffer, null);
+        }
+
+        private void WriteLog(string msg,Level level,LogFormat logFormat,Exception ex)
+        {
+            try
+            {
+                logger.Log(typeof(Logger), level, FormatLogMessage(msg, level, logFormat), ex);
+            }
+            catch
+            {
+                
+            }
+        }
+        /// <summary>
+        /// 格式化
+        /// </summary>
+        /// <param name="msg"></param>
+        /// <param name="level"></param>
+        /// <param name="logFormat"></param>
+        /// <returns></returns>
+        private string FormatLogMessage(string msg,Level level,LogFormat logFormat)
+        {
+            string str = $"[{ level.Name}]";
+            switch(logFormat)
+            {
+                case LogFormat.HostToEquipment:
+                    str = "[H->E]";
+                    break;
+                case LogFormat.EquipmentToHost:
+                    str = "[E->H]";
+                    break;
+                case LogFormat.Buffer:
+                    str = "[BUF]";
+                    break;
+
+            }
+
+            return string.Format("{0}\t{1}\t{2} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
+                    str,msg);
+        }
+    }
+}

+ 15 - 3
SecsGem.Hsms.Core/NetServer.cs

@@ -60,6 +60,10 @@ namespace SecsGem.Hsms.Core
         /// 设备Id
         /// </summary>
         private ushort _deviceId = 0;
+        /// <summary>
+        /// 是否手动停止
+        /// </summary>
+        private bool _isManualStop = false;
         #endregion
 
         #region 事件
@@ -87,6 +91,7 @@ namespace SecsGem.Hsms.Core
         /// </summary>
         public void Start(NetOptions netOptions,MessageEncoder encoder,ushort deviceId,bool manual=true)
         {
+            _isManualStop = false;
             string str = manual ? "Manual" : "Auto";
             HsmsLogMamager.Instance.Info($"{str} Start Eap System");
             this._options = netOptions;
@@ -181,6 +186,10 @@ namespace SecsGem.Hsms.Core
         /// </summary>
         public void Stop(bool manual=true)
         {
+            if (manual)
+            {
+                _isManualStop = true;
+            }
             string str = manual ? "Manual" : "Auto";
             HsmsLogMamager.Instance.Info($"{str} Disable Eap System");
             if (_server != null)
@@ -327,9 +336,12 @@ namespace SecsGem.Hsms.Core
         /// </summary>
         private void AutoStopServer()
         {
-            Stop(false);
-            Thread.Sleep(200);
-            Start(this._options, this._encoder, _deviceId, false);
+            if (!_isManualStop)
+            {
+                Stop(false);
+                Thread.Sleep(200);
+                Start(this._options, this._encoder, _deviceId, false);
+            }
         }
 
         /// <summary>