From a08829b6f60b2320555e9f560464248cc038cada Mon Sep 17 00:00:00 2001
From: xiazhengtongxue <133085197+xiazhengtongxue@users.noreply.github.com>
Date: 星期五, 13 三月 2026 13:56:16 +0800
Subject: [PATCH] fix: 修复日志问题

---
 项目代码/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs |  243 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 191 insertions(+), 52 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs"
index c3e3d19..9ffc2aa 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCSServices/WIDESEAWCS_SystemServices/Sys_LogService.cs"
@@ -131,6 +131,7 @@
 using System.Linq.Expressions;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseServices;
 using WIDESEAWCS_ISystemRepository;
 using WIDESEAWCS_ISystemServices;
@@ -149,12 +150,12 @@
 
         private const string DbName = "WIDESEA_HF";
         private const string ConnectionString = "Data Source=11.2.30.112;Initial Catalog=WIDESEA_HF;User ID=kuka;Password=kuka;Integrated Security=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
-        private const long MaxMdfSizeBytes = 8L * 1024 * 1024 * 1024; // 8GB
+        private const long MaxMdfSizeBytes = 7L * 1024 * 1024 * 1024; // 7GB
         private const int TargetShrinkSizeMB = 4500; // 4.5GB
         private const double FragmentationThreshold = 30.0; // 30%纰庣墖鐜囬槇鍊�
         private const int MaintenanceIntervalDays = 15; // 15澶╂墽琛屼竴娆�
         private const int BackupRetentionDays = 30; // 澶囦唤淇濈暀30澶�
-        private const string BackupBasePath = @"D:\DatabaseBackups"; // 澶囦唤鏂囦欢瀛樺偍鐩綍
+        private static readonly string BackupBasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DatabaseBackups");  // 澶囦唤鏂囦欢瀛樺偍鐩綍
         private const bool UseBackupCompression = true; // 鏄惁浣跨敤澶囦唤鍘嬬缉
         private const bool VerifyBackup = true; // 鏄惁楠岃瘉澶囦唤
         private const int MaxBackupRetryCount = 3; // 鏈�澶ч噸璇曟鏁�
@@ -163,7 +164,7 @@
         public Sys_LogService(ISys_LogRepository BaseDal) : base(BaseDal)
         {
             // 纭繚澶囦唤鐩綍瀛樺湪
-            EnsureBackupDirectory();
+            //EnsureBackupDirectory();
         }
 
         private void EnsureBackupDirectory()
@@ -196,19 +197,20 @@
                 }
                 try
                 {
-                    // 璁$畻3涓湀鍓嶇殑鏃ユ湡
-                    var threeMonthsAgo = DateTime.Now.AddMonths(-3);
-                    // 璁$畻3澶╁墠鐨勬棩鏈�
-                    var threeDaysAgo = DateTime.Now.AddDays(-3);
-                    // 鐗瑰畾URL
-                    var specificUrl = "http://11.2.30.141:10870/interfaces/api/amr/robotQuery";
-                    var anonUrl = "http://11.2.30.111/anon/api/kuka/drivestatus";
+                    var result = BaseDal.Db.Deleteable<Sys_Log>().ExecuteCommand();
+                    //// 璁$畻3涓湀鍓嶇殑鏃ユ湡
+                    //var threeMonthsAgo = DateTime.Now.AddMonths(-3);
+                    //// 璁$畻3澶╁墠鐨勬棩鏈�
+                    //var threeDaysAgo = DateTime.Now.AddDays(-3);
+                    //// 鐗瑰畾URL
+                    //var specificUrl = "http://11.2.30.141:10870/interfaces/api/amr/robotQuery";
+                    //var anonUrl = "http://11.2.30.111/anon/api/kuka/drivestatus";
 
-                    // 鏋勫缓鍒犻櫎鏉′欢锛�3涓湀鍓嶇殑鏃ュ織 鎴� (3澶╁墠涓擴RL涓虹壒瀹氬湴鍧�鐨勬棩蹇�)
-                    var result = BaseDal.Db.Deleteable<Sys_Log>()
-                        .Where(log => log.EndDate < threeMonthsAgo ||
-                                     (log.EndDate < threeDaysAgo && (log.Url == specificUrl || log.Url == anonUrl)))
-                        .ExecuteCommand();
+                    //// 鏋勫缓鍒犻櫎鏉′欢锛�3涓湀鍓嶇殑鏃ュ織 鎴� (3澶╁墠涓擴RL涓虹壒瀹氬湴鍧�鐨勬棩蹇�)
+                    //var result = BaseDal.Db.Deleteable<Sys_Log>()
+                    //    .Where(log => log.EndDate < threeMonthsAgo ||
+                    //                 (log.EndDate < threeDaysAgo && (log.Url == specificUrl || log.Url == anonUrl)))
+                    //    .ExecuteCommand();
                     // 鏇存柊鏈�鍚庢墽琛屾棩鏈熶负浠婂ぉ
                     _lastExecutionDate = today;
                 }
@@ -237,7 +239,7 @@
                     connection.Open();
 
                     // 1. 澶囦唤鏁版嵁搴�
-                    BackupDatabase();
+                    //BackupDatabase();
 
                     // 2. 妫�鏌ユ暟鎹簱鏂囦欢澶у皬
                     CheckDatabaseFileSize(connection);
@@ -249,7 +251,7 @@
                     UpdateStatistics(connection);
 
                     // 5. 娓呯悊鏃у浠�
-                    CleanupOldBackups();
+                    //CleanupOldBackups();
 
                     // 鏇存柊鏈�鍚庢鏌ユ棩鏈�
                     lock (_fileSizeLock)
@@ -307,14 +309,6 @@
                                 fileCount = Convert.ToInt32(reader["FileCount"]);
                             }
                         }
-
-
-                        // 妫�鏌ョ鐩樼┖闂存槸鍚﹁冻澶�
-                        if (!CheckDiskSpace(backupFilePath, totalSizeMB))
-                        {
-                            throw new Exception("纾佺洏绌洪棿涓嶈冻锛屾棤娉曟墽琛屽浠�");
-                        }
-
                         // 鏋勫缓澶囦唤SQL璇彞
                         var backupSql = BuildBackupSql(backupFilePath);
 
@@ -337,30 +331,6 @@
                     Console.WriteLine($"鏁版嵁搴撳浠藉け璐�: {ex.Message}");
                     throw new Exception($"鏁版嵁搴撳浠藉け璐�: {ex.Message}", ex);
                 }
-            }
-        }
-
-        private bool CheckDiskSpace(string backupFilePath, double estimatedBackupSizeMB)
-        {
-            try
-            {
-                var driveInfo = new DriveInfo(Path.GetPathRoot(backupFilePath));
-                var freeSpaceMB = driveInfo.AvailableFreeSpace / (1024 * 1024);
-
-                // 闇�瑕佽嚦灏�1.5鍊嶇殑绌洪棿鏉ョ‘淇濆浠介『鍒╄繘琛�
-                var requiredSpaceMB = estimatedBackupSizeMB * 1.5;
-
-                if (freeSpaceMB < requiredSpaceMB)
-                {
-                    return false;
-                }
-
-                return true;
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"妫�鏌ョ鐩樼┖闂存椂鍑洪敊: {ex.Message}");
-                return false;
             }
         }
 
@@ -507,9 +477,8 @@
                 {
                     return false;
                 }
-
                 // 妫�鏌ユ椂闂存槸鍚︽槸1鐐瑰乏鍙筹紙鍏佽1:00-1:59锛�
-                if (now.Hour != 1)
+                if (now.Hour != 1 && now.Hour != 2)
                 {
                     return false;
                 }
@@ -767,5 +736,175 @@
                 throw;
             }
         }
-    }
+
+
+
+
+            /// <summary>
+            /// 閫掑綊鑾峰彇鏂囦欢淇℃伅
+            /// </summary>
+            /// <param name="dirPath"></param>
+            /// <returns></returns>
+            private DirInfo GetDirInfo(string dirPath)
+            {
+                //褰撳墠鏂囦欢澶�
+                var dirInfo = new DirInfo();
+                dirInfo.dirName = Path.GetFileName(dirPath);
+
+                //瀛愭枃浠�
+                List<FileDataInfo> files = new List<FileDataInfo>();
+                foreach (var file in Directory.GetFiles(dirPath))
+                {
+                    files.Add(new FileDataInfo()
+                    {
+                        filePath = file,
+                        fileName = Path.GetFileName(file)
+                    });
+                }
+
+                //瀛愭枃浠跺す
+                var dirs = Directory.GetDirectories(dirPath);
+                dirInfo.dirs = new List<DirInfo>();
+                foreach (var dir in dirs)
+                {
+                    dirInfo.dirs.Add(GetDirInfo(dir));
+                }
+
+                //瀛愭枃浠跺す锛屼笌瀛愮洰褰曞悎骞�
+                foreach (var file in files)
+                {
+                    dirInfo.dirs.Add(new DirInfo() { dirPath = file.filePath, dirName = file.fileName });
+                }
+                return dirInfo;
+            }
+
+            /// <summary>
+            /// 鑾峰彇鏃ュ織鏂囦欢鍒楄〃
+            /// </summary>
+            /// <returns></returns>
+            public WebResponseContent GetLogList()
+            {
+                WebResponseContent content = new WebResponseContent();
+                string path = Path.Combine(AppContext.BaseDirectory, "log");
+                if (!Directory.Exists(path))
+                {
+                    return content.Error("鏆傛棤鏃ュ織鏂囦欢");
+                }
+
+                content.Data = GetDirInfo(path);
+                content.OK();
+                return content;
+            }
+
+            /// <summary>
+            /// 鑾峰彇鏂囦欢鍐呭
+            /// </summary>
+            /// <param name="parm"></param>
+            /// <returns></returns>
+            public WebResponseContent GetLogData(GetLogParm parm)
+            {
+                WebResponseContent content = new WebResponseContent();
+                string res = "";
+                //鏄惁璇诲彇鍒版渶鍚�
+                bool isEnd = false;
+                long startIndex = 0;
+                //鏂囦欢澶у皬
+                long len = 0;
+                try
+                {
+                if (!File.Exists(parm.path))
+                {
+                        throw new Exception($"鏂囦欢{parm.path}涓嶅瓨鍦紒");
+                    }
+                    using (FileStream fs = new FileStream(parm.path, FileMode.Open, FileAccess.Read, FileShare.Read))
+                    {
+                        //鏈�澶ц鍙栧ぇ灏�
+                        int maxsize = (int)(1024 * parm.maxsize_KB);
+                        len = fs.Length;
+                        long startPos = (long)(len * (parm.percent / 100));//璧峰浣嶇疆
+                        long readLen = len - startPos;//璇诲彇闀垮害
+
+                        //鍚戝墠鍔犺浇
+                        if (parm.topStartPos != 0)
+                        {
+                            startPos = parm.topStartPos - maxsize;
+                            if (startPos < 0)
+                            {
+                                //宸茶鍒拌捣濮嬩綅
+                                startPos = 0;
+                                readLen = parm.topStartPos;
+                            }
+                            else
+                            {
+                                readLen = maxsize;
+                            }
+                        }
+                        else
+                        {
+                            //璇诲彇澶у皬鏄惁瓒呭嚭鏈�澶ч暱搴�
+                            if (readLen > maxsize)
+                            {
+                                readLen = maxsize;
+                            }
+                            else
+                            {
+                                isEnd = true;
+                            }
+                        }
+
+                        //鍔犺浇100%锛屾寜鏈�澶у唴瀹硅鍙�
+                        if (parm.percent == 100)
+                        {
+                            if (len < maxsize)
+                            {
+                                startPos = 0;
+                                readLen = len;
+                            }
+                            else
+                            {
+                                startPos = len - maxsize;
+                                readLen = maxsize;
+                            }
+                        }
+
+                        fs.Seek(startPos, SeekOrigin.Begin);
+                        var buffer = new byte[readLen];
+                        fs.Read(buffer, 0, (int)readLen);
+
+                        startIndex = startPos;
+                        if (startPos != 0 && (parm.percent != 0 || parm.topStartPos != 0))
+                        {
+                            //涓嶆槸浠庡ご鍔犺浇锛屽垹闄ゅ彲鑳戒笉瀹屾暣鐨勭涓�琛�
+                            int skipCount = 0;
+                            for (int i = 0; i < buffer.Length; i++)
+                            {
+                                if (buffer[i] == 10)
+                                {
+                                    skipCount = i;
+                                    break;
+                                }
+                            }
+                            if (skipCount != 0)
+                            {
+                                //鍘绘帀鎹㈣
+                                skipCount++;
+                                //涓嬫璇诲彇鍓嶅欢
+                                startIndex += skipCount;
+                            }
+                            res = Encoding.UTF8.GetString(buffer.Skip(skipCount).ToArray());
+                        }
+                        else
+                        {
+                            res = Encoding.UTF8.GetString(buffer);
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    return content.Error(ex.Message);
+                }
+                return content.OK(data: new { content = res, isEnd, startIndex, len });
+            }
+        }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3