完善查询日志的功能
This commit is contained in:
parent
5698acc185
commit
877cf4ba7a
|
@ -18,6 +18,11 @@ public class TimeUtil
|
||||||
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
|
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DateTime ConvertTime(long time)
|
||||||
|
{
|
||||||
|
return DateTimeOffset.FromUnixTimeMilliseconds(time).DateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 格式化时间
|
/// 格式化时间
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
using MasstransferCommon.Model.Entity;
|
using MasstransferCommon.Model.Entity;
|
||||||
using MasstransferCommon.Utils;
|
using MasstransferCommon.Utils;
|
||||||
using MasstransferCommunicate.Minio;
|
using MasstransferCommunicate.Minio;
|
||||||
|
using MasstransferCommunicate.Mqtt.Client;
|
||||||
|
using MasstransferExporter.LogExporter.Model;
|
||||||
using MasstransferInfrastructure.Database.Sqlite;
|
using MasstransferInfrastructure.Database.Sqlite;
|
||||||
|
|
||||||
namespace MasstransferExporter.LogExporter;
|
namespace MasstransferExporter.LogExporter;
|
||||||
|
@ -12,7 +14,46 @@ public class LogFileExporter
|
||||||
|
|
||||||
private static readonly string[] Levels = ["debug", "info", "warning", "error"];
|
private static readonly string[] Levels = ["debug", "info", "warning", "error"];
|
||||||
|
|
||||||
private static MinioHelper _minio = MinioHelper.GetInstance();
|
private static readonly MinioHelper Minio = MinioHelper.GetInstance();
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 监听查询日志事件
|
||||||
|
/// </summary>
|
||||||
|
private static async Task ListenQueryLogEvent()
|
||||||
|
{
|
||||||
|
await MessageQueueHelper.Subscribe(Topics.QueryLogFile, HandleQueryLogEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理查询日志的请求事件
|
||||||
|
/// </summary>
|
||||||
|
private static async Task HandleQueryLogEvent(string topic, string payload)
|
||||||
|
{
|
||||||
|
if (payload == null) return;
|
||||||
|
|
||||||
|
var queryLogDto = JsonUtil.FromJson<QueryLogDTO>(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<FileInfo> 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()
|
public static async Task ExportLogFile()
|
||||||
{
|
{
|
||||||
|
@ -35,7 +76,7 @@ public class LogFileExporter
|
||||||
{
|
{
|
||||||
var fileName = $"{Constants.SN}/{TimeUtil.FormatTime(yesterday, "yyyyMMdd")}/{level}/{file.Name}";
|
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);
|
foreach (var subdir in directory.GetDirectories()) GetLogFiles(subdir, level, files, yesterday);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重新加载日志文件列表
|
||||||
|
/// </summary>
|
||||||
|
private static void FilterLogFiles(DirectoryInfo directory, string level, long startTime, long endTime,
|
||||||
|
List<FileInfo> 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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
namespace MasstransferExporter.LogExporter.Model;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询日志请求
|
||||||
|
/// </summary>
|
||||||
|
public class QueryLogDTO
|
||||||
|
{
|
||||||
|
public long StartTime { get; set; }
|
||||||
|
|
||||||
|
public long EndTime { get; set; }
|
||||||
|
|
||||||
|
public string Module { get; set; }
|
||||||
|
}
|
Loading…
Reference in New Issue