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