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 作为历史根节点
---
SimpleUtil/HMI.LogUtil/LogHelper.cs | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 112 insertions(+), 0 deletions(-)
diff --git a/SimpleUtil/HMI.LogUtil/LogHelper.cs b/SimpleUtil/HMI.LogUtil/LogHelper.cs
new file mode 100644
index 0000000..007182b
--- /dev/null
+++ b/SimpleUtil/HMI.LogUtil/LogHelper.cs
@@ -0,0 +1,112 @@
+锘縰sing HMI.Simple.Util;
+using System;
+using System.Data.SQLite;
+using System.IO;
+
+namespace HMI.LogUtil
+{
+ public class LogHelper
+ {
+ private static string FileName = "log";
+ private static string createTableSql = @"CREATE TABLE IF NOT EXISTS log (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ level TEXT NOT NULL,
+ text TEXT NOT NULL,
+ source TEXT,
+ time TIME
+ );";
+ /// <summary>
+ /// 闃叉澶氱嚎绋嬪垱寤烘枃浠讹紙浣嗘槸涓嶇煡閬搕eststand浼氫笉浼氬杩涚▼璋冪敤锛�
+ /// </summary>
+ private static object fileLock = new object();
+
+ private static string GetConnectionStr(string dbFile)
+ {
+ return $"DataSource={dbFile};Version=3;Pooling=False;Max Pool Size=100";
+ }
+
+ private static string GetDbFile()
+ {
+ string value = RegUtil.GetLocaltion();
+ string path = Path.Combine(value, "Config", "LogViewer", $"{FileName}_{DateTime.Now:yyyy-MM-dd}.db");
+ if (!File.Exists(path))
+ {
+ CreateDbFile(path);
+ }
+ return path;
+ }
+
+ private static void CreateDbFile(string dbFile)
+ {
+ lock (fileLock)
+ {
+ if (!File.Exists(dbFile))
+ {
+ string dir = Path.GetDirectoryName(dbFile);
+ if (!Directory.Exists(dir))
+ {
+ Directory.CreateDirectory(dir);
+ }
+ SQLiteConnection.CreateFile(dbFile);
+
+ using (SQLiteConnection conn = new SQLiteConnection())
+ using (SQLiteCommand cmd = new SQLiteCommand(conn))
+ {
+ conn.ConnectionString = GetConnectionStr(dbFile);
+ conn.Open();
+ cmd.CommandText = createTableSql;
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ }
+
+ private static bool DoLog(string level, string source, string content, out string errMsg)
+ {
+ errMsg = string.Empty;
+ try
+ {
+ string dbFile = GetDbFile();
+ using (SQLiteConnection conn = new SQLiteConnection())
+ using (SQLiteCommand cmd = new SQLiteCommand(conn))
+ {
+ conn.ConnectionString = GetConnectionStr(dbFile);
+ conn.Open();
+ cmd.CommandText =
+ $@"INSERT INTO log (level, text, source, time)
+VALUES ('{level}', '{content}', '{source}', '{DateTime.Now:yyyy-MM-dd HH:mm:ss}');";
+ if (cmd.ExecuteNonQuery() > 0)
+ {
+ return true;
+ }
+ errMsg = "鍐欏叆鏃ュ織澶辫触";
+ return false;
+ }
+ }
+ catch (Exception ex)
+ {
+ errMsg = ex.Message;
+ return false;
+ }
+ }
+
+ public static bool Debug(string source, string content, out string errMsg)
+ {
+ return DoLog("Debug", source, content, out errMsg);
+ }
+
+ public static bool Info(string source, string content, out string errMsg)
+ {
+ return DoLog("Info", source, content, out errMsg);
+ }
+ public static bool Warn(string source, string content, out string errMsg)
+ {
+ return DoLog("Warn", source, content, out errMsg);
+ }
+
+ public static bool Error(string source, string content, out string errMsg)
+ {
+ return DoLog("Error", source, content, out errMsg);
+ }
+ }
+}
--
Gitblit v1.9.1