diff --git a/MasstransferCommon/Model/Entity/MinioParams.cs b/MasstransferCommon/Model/Entity/MinioParams.cs new file mode 100644 index 0000000..4d8a4b6 --- /dev/null +++ b/MasstransferCommon/Model/Entity/MinioParams.cs @@ -0,0 +1,20 @@ +using System.ComponentModel; +using SQLite; + +namespace MasstransferCommon.Model.Entity; + +[Table("minio_params"), Description("MinIO参数")] +public class MinioParams : Entity +{ + [Column("minio_access_key"), Description("Minio AccessKey")] + public string MinioAccessKey { get; set; } + + [Column("minio_secret_key"), Description("Minio SecretKey")] + public string MinioSecretKey { get; set; } + + [Column("minio_bucket"), Description("Minio Bucket")] + public string MinioBucket { get; set; } + + [Column("minio_endpoint"), Description("Minio Endpoint")] + public string MinioEndpoint { get; set; } +} \ No newline at end of file diff --git a/MasstransferCommon/Model/Entity/MqttParams.cs b/MasstransferCommon/Model/Entity/MqttParams.cs new file mode 100644 index 0000000..6ad948b --- /dev/null +++ b/MasstransferCommon/Model/Entity/MqttParams.cs @@ -0,0 +1,19 @@ +using System.ComponentModel; +using SQLite; + +namespace MasstransferCommon.Model.Entity; + +[Table("mqtt_params"), Description("Mqtt连接参数")] +public class MqttParams : Entity +{ + [Column("server_address"), Description("服务器地址")] + public string ServerAddress { get; set; } + + [Column("port"), Description("端口")] public int Port { get; set; } + + [Column("user_name"), Description("用户名")] + public string UserName { get; set; } + + [Column("password"), Description("密码")] + public string Password { get; set; } +} \ No newline at end of file diff --git a/MasstransferCommon/Utils/TimeUtil.cs b/MasstransferCommon/Utils/TimeUtil.cs index 361324b..c1fd4a5 100644 --- a/MasstransferCommon/Utils/TimeUtil.cs +++ b/MasstransferCommon/Utils/TimeUtil.cs @@ -4,12 +4,11 @@ namespace MasstransferCommon.Utils; public class TimeUtil { - public static void Sleep(int milliseconds) { Thread.Sleep(milliseconds); } - + /// /// 获取当前的时间戳 /// @@ -32,6 +31,12 @@ public class TimeUtil return dateTime.ToString(format); } + + public static string FormatTime(DateTime dateTime, string format = "yyyy-MM-dd HH:mm:ss") + { + return dateTime.ToString(format); + } + public static string ToTimeSpan(long time) { // 使用TimeSpan.FromMilliseconds来创建TimeSpan对象 diff --git a/MasstransferExporter/LogExporter/LogFileExporter.cs b/MasstransferExporter/LogExporter/LogFileExporter.cs index 4d3195c..922782c 100644 --- a/MasstransferExporter/LogExporter/LogFileExporter.cs +++ b/MasstransferExporter/LogExporter/LogFileExporter.cs @@ -1,11 +1,63 @@ -namespace MasstransferExporter.LogExporter; +using MasstransferCommon.Model.Entity; +using MasstransferCommon.Utils; +using MasstransferCommunicate.Minio; +using MasstransferInfrastructure.Database.Sqlite; + +namespace MasstransferExporter.LogExporter; public class LogFileExporter { - public async Task Export(string logFilePath) + private static readonly SqliteHelper Helper = SqliteHelper.GetInstance(); + + private static readonly string[] Levels = ["debug", "info", "warning", "error"]; + + private static MinioHelper _minio = MinioHelper.GetInstance(); + + public static async Task ExportLogFile() { - - - + var logParams = Helper.Query("select * from log_params limit 1").FirstOrDefault(); + + var yesterday = DateTime.Today.AddDays(-1); + + var path = + "C:\\workspace\\code_repos\\haiju\\MasstransferHost\\MasstransferDesktop\\bin\\Debug\\net7.0"; + + foreach (var level in Levels) + { + List files = []; + + var directory = new DirectoryInfo(path + "\\logs"); + + GetLogFiles(directory, level, files, yesterday); + + foreach (var file in files) + { + var fileName = $"sn/{TimeUtil.FormatTime(yesterday, "yyyy-MM-dd")}/{level}/{file.Name}"; + + await _minio.UploadFileAsync("log", fileName, file.FullName); + } + } + } + + /// + /// 重新加载日志文件列表 + /// + private static void GetLogFiles(DirectoryInfo directory, string level, List files, DateTime yesterday) + { + // 获取文件夹下所有文件的信息 + var fs = directory.GetFiles(); + + var directoryName = directory.Name; + + if (!directoryName.Equals("logs") && !directoryName.Equals(level)) + { + return; + } + + // 遍历所有文件 + files.AddRange(fs.Where(f => f.CreationTime.Date.Equals(yesterday))); + + // 递归遍历当前目录下的所有文件夹 + foreach (var subdir in directory.GetDirectories()) GetLogFiles(subdir, level, files, yesterday); } } \ No newline at end of file diff --git a/MasstransferExporter/MasstransferExporter.csproj b/MasstransferExporter/MasstransferExporter.csproj index 25d8340..8bd3601 100644 --- a/MasstransferExporter/MasstransferExporter.csproj +++ b/MasstransferExporter/MasstransferExporter.csproj @@ -5,6 +5,7 @@ net7.0 enable enable + preview diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index dcc707d..a043d47 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -15,7 +15,7 @@ class Program await MessageQueueHelper.InitConnect(options); - await OperationLogExporter.ExportOperationLog(); + await LogFileExporter.ExportLogFile(); Console.WriteLine("按任意键退出"); diff --git a/MasstransferInfrastructure/Minio/MinioHelper.cs b/MasstransferInfrastructure/Minio/MinioHelper.cs index 7f0a86e..0fab250 100644 --- a/MasstransferInfrastructure/Minio/MinioHelper.cs +++ b/MasstransferInfrastructure/Minio/MinioHelper.cs @@ -1,4 +1,6 @@ -using Minio; +using MasstransferCommon.Model.Entity; +using MasstransferInfrastructure.Database.Sqlite; +using Minio; using Minio.DataModel.Args; namespace MasstransferCommunicate.Minio; @@ -10,7 +12,13 @@ public class MinioHelper { private readonly IMinioClient _client; - public MinioHelper(string endpoint, string accessKey, string secretKey) + private static MinioHelper? _instance; + + private static readonly object Lock = new(); + + private static SqliteHelper _sqliteHelper = SqliteHelper.GetInstance(); + + private MinioHelper(string endpoint, string accessKey, string secretKey) { _client = new MinioClient() .WithEndpoint(endpoint) @@ -18,6 +26,16 @@ public class MinioHelper .Build(); } + public static MinioHelper GetInstance() + { + lock (Lock) + { + var minio = _sqliteHelper.Query("select * from minio_params limit 1").FirstOrDefault(); + _instance ??= new MinioHelper(minio.MinioEndpoint, minio.MinioAccessKey, minio.MinioSecretKey); + return _instance; + } + } + /// /// 判断bucket 是否存在 ///