Ver código fonte

添加硬件日志

wb_han 5 anos atrás
pai
commit
f3fd19de7b
2 arquivos alterados com 96 adições e 1 exclusões
  1. 1 0
      FileManager/FileManager.csproj
  2. 95 1
      FileManager/LogManager.cs

+ 1 - 0
FileManager/FileManager.csproj

@@ -35,6 +35,7 @@
     <Reference Include="System" />
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Core" />
     <Reference Include="System.Drawing" />
     <Reference Include="System.Drawing" />
+    <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="Microsoft.CSharp" />

+ 95 - 1
FileManager/LogManager.cs

@@ -7,6 +7,7 @@ using System.Linq;
 using System.Reflection;
 using System.Reflection;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using System.Windows.Forms;
 
 
 namespace FileManager
 namespace FileManager
 {
 {
@@ -23,6 +24,21 @@ namespace FileManager
         /// </summary>
         /// </summary>
         private static bool runState = false;
         private static bool runState = false;
         protected static bool RunState { get => runState; set => runState = value; }
         protected static bool RunState { get => runState; set => runState = value; }
+        /// <summary>
+        /// 日志名称,按照当前时间生成
+        /// </summary>
+        protected string LogName { get => logName; set => logName = value; }
+        /// <summary>
+        /// 硬件日志文件路径
+        /// </summary>
+        protected static string HardwareLogPath { get => hardwareLogPath; set => hardwareLogPath = value; }
+
+        //默认日志路径
+        private static string defaultPath = @"\Log\";
+        //硬件日志文件路径
+        private static string hardwareLogPath = string.Empty;
+        //硬件日志名称
+        private static string logName = string.Empty;
         #endregion
         #endregion
 
 
         #region 初始化创建日志文件
         #region 初始化创建日志文件
@@ -34,6 +50,15 @@ namespace FileManager
         {
         {
             CreateFile(xmlFilePath);
             CreateFile(xmlFilePath);
         }
         }
+        /// <summary>
+        /// 初始化记录硬件操作日志文件,默认创建日志文件
+        /// </summary>
+        public static void InitHardwareLog()
+        {
+            //硬件日志名称
+            logName = DateTime.Now.ToString("yyyyMMddHHmmss")+ "HardwareLog.txt";
+            CreateHardwareFile(logName);
+        }
         #endregion
         #endregion
 
 
         #region 添加记录日志
         #region 添加记录日志
@@ -78,6 +103,29 @@ namespace FileManager
             st = null;
             st = null;
             LogWrite(FileName, MethodName, LineNumber, logErrorContent, RunState);
             LogWrite(FileName, MethodName, LineNumber, logErrorContent, RunState);
         }
         }
+
+        /// <summary>
+        /// 添加硬件运行日志
+        /// </summary>
+        /// <param name="logContent">日志内容</param>
+        /// <param name="logState">True:正常日志 False:错误日志</param>
+        public static void AddHardwareLog(string logContent,bool logState)
+        {
+            //获取调用当前方法的页面与代码行数
+            StackTrace st = new StackTrace(new StackFrame(1, true));
+            StackFrame sf = st.GetFrame(0);
+            //文件名称
+            string FileName = sf.GetFileName();
+            //方法名
+            string MethodName = sf.GetMethod().Name;
+            //行号
+            int LineNumber = sf.GetFileLineNumber();
+            //设置运行状态
+            RunState = true;
+            st = null;
+
+            HardwareLogWrite(FileName, MethodName, LineNumber, logContent, RunState);
+        }
         #endregion
         #endregion
 
 
         #region 读取日志信息 返回DataTable
         #region 读取日志信息 返回DataTable
@@ -182,6 +230,25 @@ namespace FileManager
             }
             }
             return false;
             return false;
         }
         }
+
+        protected static bool CreateHardwareFile(string logName)
+        {
+            string DirectoryPath = Application.StartupPath + defaultPath;
+            if (!Directory.Exists(DirectoryPath))
+            {
+                Directory.CreateDirectory(DirectoryPath);
+            }
+            HardwareLogPath = DirectoryPath + logName;
+            //判断是否存在日志路径
+            if (!IsExistFile(HardwareLogPath))
+            {
+                //没有则创建这个文件
+                FileStream fs1 = new FileStream(HardwareLogPath, FileMode.Create, FileAccess.Write);
+                fs1.Close();
+                return true;
+            }
+            return false;
+        }
         /// <summary>
         /// <summary>
         /// 判断是否存在日志文件
         /// 判断是否存在日志文件
         /// </summary>
         /// </summary>
@@ -222,7 +289,34 @@ namespace FileManager
                 return;
                 return;
             }
             }
         }
         }
+
+        protected static void HardwareLogWrite(string FileName, string MethodName, int LineNumber, string logContent, bool logState)
+        {
+            try
+            {
+                //记录日志时间
+                string currTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
+                //格式:(日志类型+日志记录的所在文件名称+方法名称+行号+时间+描述+状态)
+                string logType = "Trace";
+                if (!logState)
+                {
+                    logType = "Error";
+                }
+                //日志类型:LogType:Trace 文件名:xxxx.cs 方法名:aaa() 行数:10 时间:2020-XX-XX XX:XX:XX 描述:切点名称-操作描述 状态:True 
+                string logStr = "LogType:" + logType + "-" + FileName + "-" + MethodName + "-" + LineNumber + "-" + currTime + "-" + logContent + "-" + logState.ToString() + "";
+                //写入日志信息
+                FileStream fs1 = new FileStream(HardwareLogPath, FileMode.Append, FileAccess.Write);//创建写入文件    
+                StreamWriter sw = new StreamWriter(fs1);
+                sw.WriteLine(logStr);//开始写入值
+                sw.Close();
+                fs1.Close();
+            }
+            catch (Exception)
+            {
+                return;
+            }
+        }
         #endregion
         #endregion
-        
+
     }
     }
 }
 }