From 85458565e09bda1044d19b13d0b1ffb7ab576857 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期五, 22 十一月 2024 16:47:40 +0800
Subject: [PATCH] 货位分配

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/Logger.cs |  237 ++++++++++++++++++++++++++++------------------------------
 1 files changed, 115 insertions(+), 122 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/Logger.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/Logger.cs"
index 4995e84..7ce4580 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/Logger.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/Logger.cs"
@@ -16,143 +16,136 @@
 {
     public class Logger
     {
-
-        public static void Debug(string message)
+        /// <summary>
+        /// 鍐欏叆璋冭瘯鏃ュ織
+        /// </summary>
+        /// <param name="fileName"></param>
+        /// <param name="msg"></param>
+        public void WriteDebug(string fileName, string msg)
         {
-
-        }
-
-        public static void Debug(string message, Exception exception)
-        {
-
-        }
-
-        public static void Info(string message)
-        {
-
-        }
-
-        public static void Info(string message, Exception exception)
-        {
-
-        }
-
-        public static void Warn(string message)
-        {
-
-        }
-
-        public static void Warning(string message, Exception exception)
-        {
-
-        }
-
-        public static void Error(string message)
-        {
-
-        }
-
-        public static void Error(string message, Exception exception)
-        {
-
-        }
-
-        public static void Fatal(string message)
-        {
-
-        }
-
-        public static void Fatal(string message, Exception exception)
-        {
-
-        }
-        public static void Write_Log(string groupName, string logName, string content, object data = null)
-        {
-            DateTime nowTime = DateTime.Now;
-            string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{nowTime.ToString("yyyy-MM-dd")}";
-            //濡傛灉鏃ュ織鏂囦欢鐩綍涓嶅瓨鍦�,鍒欏垱寤�
-            if (!Directory.Exists(basePath))
+            if (AppSettings.Get(new string[] { "LogDeubgEnable" }).ObjToBool())
             {
-                Directory.CreateDirectory(basePath);
+                StringBuilder builder = new StringBuilder(msg);
+                builder.Append(Environment.NewLine);
+                builder.Append(Environment.NewLine);
+                WriteLogToFile($"Debug_{fileName}", builder.ToString());
             }
+        }
+
+        /// <summary>
+        /// 鍐欏叆淇℃伅鏃ュ織
+        /// </summary>
+        /// <param name="fileName"></param>
+        /// <param name="msg"></param>
+        public void WriteInfo(string fileName, string msg)
+        {
+            StringBuilder builder = new StringBuilder(msg);
+            builder.Append(Environment.NewLine);
+            builder.Append(Environment.NewLine);
+            WriteLogToFile($"Info_{fileName}", builder.ToString());
+        }
+
+        /// <summary>
+        /// 鍐欏叆閿欒鏃ュ織
+        /// </summary>
+        /// <param name="fileName"></param>
+        /// <param name="msg"></param>
+        /// <param name="ex"></param>
+        public void WriteError(string fileName, string msg, Exception ex)
+        {
+            StringBuilder builder = new StringBuilder(msg);
+            builder.Append(Environment.NewLine);
+            builder.Append("閿欒淇℃伅锛�");
+            builder.Append(ex.Message);
+            builder.Append(Environment.NewLine);
+            builder.Append("閿欒婧愶細");
+            builder.Append(ex.Source);
+            builder.Append(Environment.NewLine);
+            builder.Append("閿欒鍫嗘爤锛�");
+            builder.Append(ex.StackTrace);
+            builder.Append(Environment.NewLine);
+            builder.Append("閿欒绫诲瀷锛�");
+            builder.Append(ex.GetType().ToString());
+            builder.Append(Environment.NewLine);
+            builder.Append("閿欒鏂规硶锛�");
+            builder.Append(ex.TargetSite?.ToString());
+            builder.Append(Environment.NewLine);
+            builder.Append(Environment.NewLine);
+            WriteLogToFile($"Error_{fileName}", builder.ToString());
+        }
+
+
+        ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
+        string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Log\\{DateTime.Now.ToString("yyyy-MM-dd")}");
+
+        private void WriteLogToFile(string fileName, string log)
+        {
             try
             {
-                FileStream fs = new FileStream(basePath + "/" + logName + $"{nowTime.ToString("yyMMdd")}.txt", FileMode.Append);
-                StreamWriter strwriter = new StreamWriter(fs);
-                try
+                // 杩涘叆鍐欓攣
+                LogWriteLock.EnterWriteLock();
+
+                // 濡傛灉鏂囦欢澶逛笉瀛樺湪锛屽垯鍒涘缓鏂囦欢澶�
+                if (!Directory.Exists(folderPath))
                 {
-                    strwriter.WriteLine(nowTime.ToString() + "." + nowTime.Millisecond);
-                    strwriter.WriteLine(content);
-                    if (data != null)
-                    {
-                        strwriter.WriteLine(JsonConvert.SerializeObject(data));
-                    }
-                    strwriter.WriteLine("-------------------------------");
-                    strwriter.WriteLine();
-                    strwriter.Flush();
+                    Directory.CreateDirectory(folderPath);
                 }
-                catch { }
-                finally
-                {
-                    strwriter.Close();
-                    fs.Close();
-                }
+                // 鑾峰彇鏃ュ織鏂囦欢璺緞
+                string logFilePath = Path.Combine(folderPath, GetLastAccessFileName(fileName));
+                // 鑾峰彇褰撳墠鏃堕棿
+                DateTime now = DateTime.Now;
+                // 鏋勯�犳棩蹇楀唴瀹�
+                string logContent = $"銆恵now}銆憑Environment.NewLine}{log}";
+
+                // 灏嗘棩蹇楀唴瀹硅拷鍔犲埌鏃ュ織鏂囦欢涓�
+                File.AppendAllText(logFilePath, logContent);
             }
             catch { }
+            finally
+            {
+                // 閫�鍑哄啓閿�
+                LogWriteLock.ExitWriteLock();
+            }
         }
-
-        public static void WriteApiLog2DB(HttpContext context, string requestParameter, DateTime beginDate, string responseParameter, DateTime endDate, LoggerStatus loggerStatus)
+        int size = 10 * 1024 * 1024;
+        string ext = ".log";
+        private string GetLogFilePath(string folderPath, string fileName)
         {
-            try
+            // 鑾峰彇鎸囧畾鏂囦欢澶逛笅鐨勬墍鏈夋枃浠�
+            var allFiles = new DirectoryInfo(folderPath);
+            // 鑾峰彇绗﹀悎鏉′欢鐨勬枃浠讹紝鎸夋枃浠跺悕闄嶅簭鎺掑垪
+            var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(fileName.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList();
+
+            FileInfo? file = selectFiles.FirstOrDefault();
+            // 濡傛灉鏈夌鍚堟潯浠剁殑鏂囦欢锛岃繑鍥炵涓�涓枃浠剁殑瀹屾暣璺緞
+            if (file != null)
             {
-                if (context.Request.Method == "OPTIONS") return;
-
-                if (context == null)
-                {
-                    Console.WriteLine($"鏈幏鍙栧埌httpcontext淇℃伅,reqParam:{requestParameter},respParam:{responseParameter},success:{loggerStatus}");
-                    return;
-                }
-
-                Dictionary<string, object> dic = new Dictionary<string, object>
-                {
-                    {"BeginDate",beginDate },
-                    {"ElapsedTime",(endDate - beginDate).TotalMilliseconds.DoubleToInt() },
-                    {"EndDate",endDate },
-                    {"RequestParam",requestParameter },
-                    {"ResponseParam",responseParameter },
-                    {"Success",1 },
-                    {"Url",context.Request.Path.Value??"" },
-                    {"UserIP",context.GetUserIp() },
-                    {"UserName","App.User?.Name" },
-                    {"User_Id","0" }
-
-                    //{"BeginDate",beginDate },
-                    //{"ElapsedTime",(endDate - beginDate).TotalMilliseconds.ObjToInt() },
-                    //{"EndDate",endDate },
-                    //{"RequestParam",requestParameter },
-                    //{"ResponseParam",responseParameter },
-                    //{"Success",1 },
-                    //{"Url",context.Request.Path.Value??"" },
-                    //{"UserIP",context.GetUserIp() },
-                    //{"UserName","App.User?.Name" },
-                    //{"User_Id","App.User?.ID" }
-                };
-
-
-                SqlSugarClient sqlSugarClient = DBContext.GetCustomDB(DBContext.GetConnectionConfig());
-                sqlSugarClient.Insertable(dic).AS("Sys_Log").ExecuteCommand();
+                return file.FullName;
             }
-            catch (Exception ex)
-            {
 
-            }
+            // 濡傛灉娌℃湁绗﹀悎鏉′欢鐨勬枃浠讹紝杩斿洖涓�涓柊鐨勬枃浠惰矾寰勶紝鏂囦欢鍚嶄负鍘熸枃浠跺悕鍔犱笂褰撳墠鏃堕棿
+            return Path.Combine(folderPath, $@"{fileName}_{DateTime.Now.ToString("HH-mm-ss")}.log");
         }
-    }
 
-    public enum LoggerStatus
-    {
-        Success = 1,
-        Error = 2,
-        Info = 3
+        private string GetLastAccessFileName(string fileName)
+        {
+            foreach (var m in GetExistLogFileNames(fileName))
+            {
+                FileInfo fileInfo = new FileInfo(m);
+                if (fileInfo.Length < size)
+                {
+                    return m;
+                }
+            }
+
+            // 杩斿洖涓�涓柊鐨勯粯璁ゅ綋鍓嶆椂闂寸殑鏃ュ織鍚嶇О
+            return $@"{fileName}_{DateTime.Now.ToString("HH-mm-ss")}.log";
+        }
+
+        private string[] GetExistLogFileNames(string fileName)
+        {
+            string[] fileNames = Directory.GetFiles(folderPath, fileName + "*.log");
+            return fileNames;
+        }
     }
 }

--
Gitblit v1.9.3