From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 WPFProject/Utils/CommonLogger.cs |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/WPFProject/Utils/CommonLogger.cs b/WPFProject/Utils/CommonLogger.cs
new file mode 100644
index 0000000..5232b73
--- /dev/null
+++ b/WPFProject/Utils/CommonLogger.cs
@@ -0,0 +1,106 @@
+锘縰sing HMI.Core.Api;
+using HMI.Core.Util;
+using log4net;
+using System;
+using System.IO;
+
+namespace WPFProject.Utils
+{
+    /// <summary>
+    /// 寰�sqlite鍐欐棩蹇�
+    /// </summary>
+    class CommonLogger : ILogWriter
+    {
+        private static ILog log = LogManager.GetLogger("CommonLogger");
+        private SqliteHelper sqliteHelper;
+        private DateTime currentDate;
+
+        public CommonLogger()
+        {
+            RefreshConnection();
+        }
+
+        private SqliteHelper InitHelper(DateTime date)
+        {
+            string file = Path.Combine(PathHelper.LogViewerDir, $"log_{date.ToString("yyyy-MM-dd")}.db");
+            if (!File.Exists(file))
+            {
+                SqliteHelper.CreateDatabase(file);
+            }
+            var sqliteHelper = new SqliteHelper(file);
+            sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS log (
+                    id INTEGER PRIMARY KEY AUTOINCREMENT,
+                    level TEXT NOT NULL,
+                    text TEXT NOT NULL,
+                    source TEXT,
+                    time TIME
+                    );");
+            return sqliteHelper;
+        }
+
+        private void RefreshConnection()
+        {
+            if (sqliteHelper == null)
+            {
+                currentDate = DateTime.Today;
+                sqliteHelper = InitHelper(currentDate);
+                return;
+            }
+            else
+            {
+                lock (this)
+                {
+                    try
+                    {
+                        var current = DateTime.Today;
+                        if (current == currentDate) return;
+                        sqliteHelper?.Close();
+                        sqliteHelper = null;
+                        currentDate = current;
+                        sqliteHelper = InitHelper(currentDate);
+                    }
+                    catch (Exception ex)
+                    {
+                        log.Error("RefreshConnection Failed.", ex);
+                        sqliteHelper = null;
+                    }
+                }
+            }
+        }
+
+        public void Debug(string source, string content)
+        {
+            DoLog("Debug", source, content);
+        }
+
+        public void Info(string source, string content)
+        {
+            DoLog("Info", source, content);
+        }
+        public void Warn(string source, string content)
+        {
+            DoLog("Warn", source, content);
+        }
+
+        public void Error(string source, string content)
+        {
+            DoLog("Error", source, content);
+        }
+
+        private void DoLog(string level, string source, string content)
+        {
+            if (currentDate != DateTime.Today || sqliteHelper == null)
+            {
+                RefreshConnection();
+            }
+            if (sqliteHelper == null)
+            {
+                log.Warn($"Level:{level} Source:{source} {content}");
+            }
+            else
+            {
+                sqliteHelper.ExecuteSql($"INSERT INTO log (level, text, source, time) VALUES ('{level}', '{content}', '{source}', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}');");
+            }
+        }
+    }
+}

--
Gitblit v1.9.1