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