林豪 左 преди 1 година
родител
ревизия
8ca9d69b73
променени са 1 файла, в които са добавени 27 реда и са изтрити 19 реда
  1. 27 19
      ServiceCenter/Logs/LogHub.cs

+ 27 - 19
ServiceCenter/Logs/LogHub.cs

@@ -153,7 +153,6 @@ namespace ServiceCenter.Logs
             {
                 var sw = new Stopwatch();
                 sw.Start();
-                var index = 0;
                 List<LogModel> logs = new List<LogModel>();
                 try
                 {
@@ -166,27 +165,35 @@ namespace ServiceCenter.Logs
                         logs = ret.Select(JsonConvert.DeserializeObject<LogModel>).ToList();
                     }
 
-                    //Parallel.ForEach(logs.Where(log => log != null).GroupBy(x => x.path), psth =>
-                    //{
-                    //});
-                    foreach (var path in logs.Where(log => log != null).GroupBy(x => x.path))
+                    Parallel.ForEach(logs.Where(log => log != null).GroupBy(x => x.path), paths =>
                     {
-                        foreach (var title in path.GroupBy(x => x.Title))
+                        Parallel.ForEach(paths.GroupBy(x => x.Title), title =>
                         {
                             var log = title.First();
                             var cons = title.OrderBy(x => x.Time).Select(x => x.Con);
-                            if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
-                            File.AppendAllLines(Path.Combine(log.path, log.Title), cons);
-                            index++;
-                        }
-                    }
-                    ////log = ret as LogModel;
-                    //if (log != null)
-                    //{
-                    //    if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
-
-                    //    File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
-                    //}
+                            if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);/*.txt*/
+
+                            var path = Path.Combine(log.path, log.Title);
+                            var fileInfo = new FileInfo(path);
+                            var fileMb = fileInfo.Length / 1024 / 1024;
+                            if (fileMb > 20) //如果当前文件大小超过20MB 开始封存此文件,
+                            {
+                                var fileIndex = 1; //文件索引
+                                var isSave = false;
+                                while (!isSave) //未保存成功
+                                {
+                                    var newPath = Path.Combine(log.path, log.Title.Replace(".txt", $"{fileIndex}.txt"));//使用文件索引头来判断当前索引对应的标题文件是否存在
+                                    if (!File.Exists(newPath))//不存在,将现有文件更名成新的文件名
+                                    {
+                                        File.Move(path, newPath);
+                                        isSave = true;
+                                    }
+                                    fileIndex++;
+                                }
+                            }
+                            File.AppendAllLines(path, cons);
+                        });
+                    });
                 }
                 catch (Exception ex)
                 {
@@ -196,7 +203,8 @@ namespace ServiceCenter.Logs
                 sw.Stop();
                 var path1 = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
                 if (!Directory.Exists(path1)) Directory.CreateDirectory(path1);
-                File.AppendAllText(Path.Combine(path1, "耗时.txt"), $"{DateTime.Now.yyyyMMddhhmmssf()}耗时:{sw.ElapsedMilliseconds}--{logs.Count}--{index}\n");
+                var count = logs.Where(log => log != null).GroupBy(x => x.path).SelectMany(x => x.GroupBy(t => t.Title)).Count();
+                File.AppendAllText(Path.Combine(path1, "耗时.txt"), $"{DateTime.Now.yyyyMMddhhmmssf()}耗时:{sw.ElapsedMilliseconds}--{logs.Count}--{count}\n");
                 if (sw.ElapsedMilliseconds >= toao) continue;
                 var time = toao - sw.ElapsedMilliseconds.ToInt();
                 Thread.Sleep(time);