|
|
@ -40,7 +40,8 @@ namespace TaskManager.Controllers |
|
|
|
private const int BatchSize = 100; // 批量写入大小
|
|
|
|
private readonly string _logDirectory; |
|
|
|
private readonly IServiceProvider _serviceProvider; |
|
|
|
|
|
|
|
private readonly string _requestLogDirectory; |
|
|
|
private readonly string _responseLogDirectory; |
|
|
|
private readonly List<string> list = new List<string>() { |
|
|
|
"来料检验数据", |
|
|
|
"排产数据", |
|
|
@ -68,8 +69,13 @@ namespace TaskManager.Controllers |
|
|
|
|
|
|
|
_logger = logger; |
|
|
|
_serviceProvider = serviceProvider; ; |
|
|
|
_logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomLogs"); // 使用更安全的路径获取方式
|
|
|
|
EnsureDirectoryExists(_logDirectory); |
|
|
|
|
|
|
|
_requestLogDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomLogs"); |
|
|
|
_responseLogDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomLogs", "Responses"); |
|
|
|
|
|
|
|
//_logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomLogs"); // 使用更安全的路径获取方式
|
|
|
|
EnsureDirectoryExists(_requestLogDirectory); |
|
|
|
EnsureDirectoryExists(_responseLogDirectory); |
|
|
|
} |
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
|
|
@ -99,10 +105,23 @@ namespace TaskManager.Controllers |
|
|
|
List<TaskLog> logsToSave = new List<TaskLog>(); |
|
|
|
foreach (var log in logs) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(log.Remark)) |
|
|
|
if (log.Type == "请求") |
|
|
|
{ |
|
|
|
log.Path = WriteLogToFile(log.Remark, _requestLogDirectory); |
|
|
|
} |
|
|
|
else if (log.Type == "应答") |
|
|
|
{ |
|
|
|
log.Path = WriteLogToFile(log.Remark); |
|
|
|
log.Path = WriteResponseLogToFile(log.Remark, _responseLogDirectory,log.TaskName); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
log.Path = WriteLogToFile(log.Remark, _requestLogDirectory); |
|
|
|
} |
|
|
|
|
|
|
|
//if (!string.IsNullOrEmpty(log.Remark))
|
|
|
|
//{
|
|
|
|
// log.Path = WriteLogToFile(log.Remark,log.TaskName);
|
|
|
|
//}
|
|
|
|
if (list.Contains(log.TaskName)) |
|
|
|
{ |
|
|
|
log.Module = "生产质量"; |
|
|
@ -144,13 +163,11 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
} |
|
|
|
// 修改后的日志写入方法
|
|
|
|
private string WriteLogToFile(string jsonContent) |
|
|
|
private string WriteLogToFile(string jsonContent, string baseDirectory) |
|
|
|
{ |
|
|
|
//if string.IsNullOrEmpty(logMessage.RawRemark)) return null; // 必须提供JSON数据
|
|
|
|
|
|
|
|
// 创建日期目录
|
|
|
|
string dateDirectory = DateTime.Now.ToString("yyyy-MM-dd"); |
|
|
|
string fullDatePath = Path.Combine(_logDirectory, dateDirectory); |
|
|
|
string fullDatePath = Path.Combine(baseDirectory, dateDirectory); |
|
|
|
EnsureDirectoryExists(fullDatePath); |
|
|
|
|
|
|
|
// 生成唯一文件名(时间戳+随机数)
|
|
|
@ -167,12 +184,38 @@ namespace TaskManager.Controllers |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Console.WriteLine($"JSON文件写入失败:{ex.Message}"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
private string WriteResponseLogToFile(string jsonContent, string baseDirectory, string taskName) |
|
|
|
{ |
|
|
|
// 创建日期目录
|
|
|
|
string dateDirectory = DateTime.Now.ToString("yyyy-MM-dd"); |
|
|
|
string fullDatePath = Path.Combine(baseDirectory, dateDirectory, taskName); |
|
|
|
EnsureDirectoryExists(fullDatePath); |
|
|
|
|
|
|
|
// 生成唯一文件名(时间戳+随机数)
|
|
|
|
string fileName = $"log_{DateTime.Now.Ticks}_{Random.Shared.Next(1000, 9999)}.json"; |
|
|
|
string fullPath = Path.Combine(fullDatePath, fileName); |
|
|
|
try |
|
|
|
{ |
|
|
|
// 写入文件(使用UTF-8无BOM格式)
|
|
|
|
File.WriteAllText(fullPath, jsonContent, new UTF8Encoding(false)); |
|
|
|
|
|
|
|
// 存储相对路径(从日志根目录开始,使用正斜杠兼容API)
|
|
|
|
return Path.Combine(dateDirectory,taskName, fileName).Replace('\\', '/'); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Console.WriteLine($"JSON文件写入失败:{ex.Message}"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|