From 877cf4ba7ae74758a89a91f7ed677f7e83789ca8 Mon Sep 17 00:00:00 2001 From: huangxianguo Date: Fri, 23 Aug 2024 16:23:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9F=A5=E8=AF=A2=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MasstransferCommon/Utils/TimeUtil.cs | 5 ++ .../LogExporter/LogFileExporter.cs | 71 ++++++++++++++++++- .../LogExporter/Model/QueryLogDTO.cs | 13 ++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 MasstransferExporter/LogExporter/Model/QueryLogDTO.cs diff --git a/MasstransferCommon/Utils/TimeUtil.cs b/MasstransferCommon/Utils/TimeUtil.cs index c1fd4a5..fae8d33 100644 --- a/MasstransferCommon/Utils/TimeUtil.cs +++ b/MasstransferCommon/Utils/TimeUtil.cs @@ -18,6 +18,11 @@ public class TimeUtil return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); } + public static DateTime ConvertTime(long time) + { + return DateTimeOffset.FromUnixTimeMilliseconds(time).DateTime; + } + /// /// 格式化时间 diff --git a/MasstransferExporter/LogExporter/LogFileExporter.cs b/MasstransferExporter/LogExporter/LogFileExporter.cs index 1647bcf..2fa48a4 100644 --- a/MasstransferExporter/LogExporter/LogFileExporter.cs +++ b/MasstransferExporter/LogExporter/LogFileExporter.cs @@ -2,6 +2,8 @@ using MasstransferCommon.Model.Entity; using MasstransferCommon.Utils; using MasstransferCommunicate.Minio; +using MasstransferCommunicate.Mqtt.Client; +using MasstransferExporter.LogExporter.Model; using MasstransferInfrastructure.Database.Sqlite; namespace MasstransferExporter.LogExporter; @@ -12,7 +14,46 @@ public class LogFileExporter private static readonly string[] Levels = ["debug", "info", "warning", "error"]; - private static MinioHelper _minio = MinioHelper.GetInstance(); + private static readonly MinioHelper Minio = MinioHelper.GetInstance(); + + + /// + /// 监听查询日志事件 + /// + private static async Task ListenQueryLogEvent() + { + await MessageQueueHelper.Subscribe(Topics.QueryLogFile, HandleQueryLogEvent); + } + + /// + /// 处理查询日志的请求事件 + /// + private static async Task HandleQueryLogEvent(string topic, string payload) + { + if (payload == null) return; + + var queryLogDto = JsonUtil.FromJson(payload); + + if (queryLogDto == null) return; + + var path = + "C:\\workspace\\code_repos\\haiju\\MasstransferHost\\MasstransferDesktop\\bin\\Debug\\net7.0"; + + var directory = new DirectoryInfo(path + "\\logs"); + + List files = []; + + FilterLogFiles(directory, queryLogDto.Module, queryLogDto.StartTime, queryLogDto.EndTime, files); + + foreach (var file in files) + { + var fileName = + $"{Constants.SN}/{TimeUtil.FormatTime(file.CreationTime, "yyyyMMdd")}/{queryLogDto.Module}/{file.Name}"; + + await Minio.UploadFileAsync("log", fileName, file.FullName); + } + } + public static async Task ExportLogFile() { @@ -35,7 +76,7 @@ public class LogFileExporter { var fileName = $"{Constants.SN}/{TimeUtil.FormatTime(yesterday, "yyyyMMdd")}/{level}/{file.Name}"; - await _minio.UploadFileAsync("log", fileName, file.FullName); + await Minio.UploadFileAsync("log", fileName, file.FullName); } } } @@ -61,4 +102,30 @@ public class LogFileExporter // 递归遍历当前目录下的所有文件夹 foreach (var subdir in directory.GetDirectories()) GetLogFiles(subdir, level, files, yesterday); } + + /// + /// 重新加载日志文件列表 + /// + private static void FilterLogFiles(DirectoryInfo directory, string level, long startTime, long endTime, + List files) + { + // 获取文件夹下所有文件的信息 + var fs = directory.GetFiles(); + + var directoryName = directory.Name; + + if (!directoryName.Equals("logs") && !directoryName.Equals(level)) + { + return; + } + + var start = TimeUtil.ConvertTime(startTime); + var end = TimeUtil.ConvertTime(endTime); + + // 遍历所有文件 + files.AddRange(fs.Where(f => f.CreationTime >= start && f.CreationTime <= end)); + + // 递归遍历当前目录下的所有文件夹 + foreach (var subdir in directory.GetDirectories()) FilterLogFiles(subdir, level, startTime, endTime, files); + } } \ No newline at end of file diff --git a/MasstransferExporter/LogExporter/Model/QueryLogDTO.cs b/MasstransferExporter/LogExporter/Model/QueryLogDTO.cs new file mode 100644 index 0000000..22e06c6 --- /dev/null +++ b/MasstransferExporter/LogExporter/Model/QueryLogDTO.cs @@ -0,0 +1,13 @@ +namespace MasstransferExporter.LogExporter.Model; + +/// +/// 查询日志请求 +/// +public class QueryLogDTO +{ + public long StartTime { get; set; } + + public long EndTime { get; set; } + + public string Module { get; set; } +} \ No newline at end of file