|
|
@ -675,6 +675,9 @@ namespace LTEMvcApp.Controllers |
|
|
|
var currentLogs = _webSocketManager.GetLogCache()?.ToList() ?? new List<LTELog>(); |
|
|
|
var currentLogHash = CalculateLogsHash(currentLogs); |
|
|
|
|
|
|
|
_logger.LogDebug("StreamLogs: 当前日志数量: {CurrentCount}, 上次日志数量: {LastCount}, 哈希值: {CurrentHash}", |
|
|
|
currentLogs.Count, lastLogCount, currentLogHash); |
|
|
|
|
|
|
|
// 检查是否有新日志(通过数量和内容哈希双重检查)
|
|
|
|
bool hasNewLogs = false; |
|
|
|
List<LTELog> newLogs = new List<LTELog>(); |
|
|
@ -684,6 +687,7 @@ namespace LTEMvcApp.Controllers |
|
|
|
// 数量增加,计算新增的日志
|
|
|
|
newLogs = currentLogs.Skip(lastLogCount).ToList(); |
|
|
|
hasNewLogs = newLogs.Any(); |
|
|
|
_logger.LogDebug("StreamLogs: 检测到数量增加,新增 {NewCount} 条日志", newLogs.Count); |
|
|
|
} |
|
|
|
else if (currentLogs.Count == lastLogCount && currentLogHash != lastLogHash) |
|
|
|
{ |
|
|
@ -691,6 +695,7 @@ namespace LTEMvcApp.Controllers |
|
|
|
// 比较每个日志项,找出变化的日志
|
|
|
|
newLogs = GetChangedLogs(lastLogs, currentLogs); |
|
|
|
hasNewLogs = newLogs.Any(); |
|
|
|
_logger.LogDebug("StreamLogs: 检测到内容变化,变化 {ChangedCount} 条日志", newLogs.Count); |
|
|
|
} |
|
|
|
else if (currentLogs.Count < lastLogCount) |
|
|
|
{ |
|
|
@ -707,6 +712,12 @@ namespace LTEMvcApp.Controllers |
|
|
|
lastLogHash = currentLogHash; |
|
|
|
continue; // 跳过本次循环,等待下次检查
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 数量和内容都没有变化
|
|
|
|
_logger.LogDebug("StreamLogs: 没有检测到变化,当前数量: {CurrentCount}, 上次数量: {LastCount}", |
|
|
|
currentLogs.Count, lastLogCount); |
|
|
|
} |
|
|
|
|
|
|
|
if (hasNewLogs && newLogs.Any()) |
|
|
|
{ |
|
|
@ -1043,6 +1054,7 @@ namespace LTEMvcApp.Controllers |
|
|
|
{ |
|
|
|
var logs = _webSocketManager.GetLogCache()?.ToList() ?? new List<LTELog>(); |
|
|
|
var logCount = _webSocketManager.GetLogCacheCount(); |
|
|
|
var cacheStatus = _webSocketManager.GetLogCacheStatus(); |
|
|
|
|
|
|
|
var debugInfo = new |
|
|
|
{ |
|
|
@ -1054,10 +1066,10 @@ namespace LTEMvcApp.Controllers |
|
|
|
{ |
|
|
|
timestamp = log.Timestamp, |
|
|
|
layer = log.Layer, |
|
|
|
//level = log.Level,
|
|
|
|
message = log.Message?.Substring(0, Math.Min(100, log.Message?.Length ?? 0)) + "..." |
|
|
|
}).ToList(), |
|
|
|
logHash = CalculateLogsHash(logs) |
|
|
|
logHash = CalculateLogsHash(logs), |
|
|
|
cacheStatus = cacheStatus |
|
|
|
}; |
|
|
|
|
|
|
|
return Ok(debugInfo); |
|
|
@ -1103,6 +1115,71 @@ namespace LTEMvcApp.Controllers |
|
|
|
return StatusCode(500, new { message = "获取连接状态失败", error = ex.Message }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 测试SSE连接(调试用)
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>测试结果</returns>
|
|
|
|
[HttpGet("logs/test-connection")] |
|
|
|
public ActionResult<object> TestSseConnection() |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var testResult = new |
|
|
|
{ |
|
|
|
message = "SSE连接测试成功", |
|
|
|
timestamp = DateTime.UtcNow, |
|
|
|
serverTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), |
|
|
|
logCacheCount = _webSocketManager.GetLogCacheCount(), |
|
|
|
testData = new { test = true, message = "这是一个测试消息" } |
|
|
|
}; |
|
|
|
|
|
|
|
return Ok(testResult); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "测试SSE连接时发生错误"); |
|
|
|
return StatusCode(500, new { message = "测试连接失败", error = ex.Message }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 强制推送测试日志(调试用)
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>操作结果</returns>
|
|
|
|
[HttpPost("logs/force-push-test")] |
|
|
|
public ActionResult ForcePushTestLogs() |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var testLogs = new List<LTELog> |
|
|
|
{ |
|
|
|
new LTELog |
|
|
|
{ |
|
|
|
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
|
|
|
Layer = "TEST", |
|
|
|
Level = "debug", |
|
|
|
Message = $"强制推送测试日志 - {DateTime.Now:HH:mm:ss.fff}", |
|
|
|
Client = null |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// 手动添加到日志缓存
|
|
|
|
_webSocketManager.AddLogsToCache(testLogs); |
|
|
|
|
|
|
|
_logger.LogInformation("强制推送测试日志: {Message}", testLogs[0].Message); |
|
|
|
|
|
|
|
return Ok(new { |
|
|
|
message = $"已强制推送测试日志: {testLogs[0].Message}", |
|
|
|
timestamp = testLogs[0].Timestamp |
|
|
|
}); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "强制推送测试日志时发生错误"); |
|
|
|
return StatusCode(500, new { message = "强制推送失败", error = ex.Message }); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|