Browse Source

66666

feature/LteClientLogFun
root 1 month ago
parent
commit
56febf40e4
  1. 81
      LTEMvcApp/Controllers/WebSocketController.cs
  2. 38
      LTEMvcApp/Services/WebSocketManagerService.cs
  3. 402
      LTEMvcApp/test_client_config.json

81
LTEMvcApp/Controllers/WebSocketController.cs

@ -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>

38
LTEMvcApp/Services/WebSocketManagerService.cs

@ -494,14 +494,21 @@ namespace LTEMvcApp.Services
// 使用线程安全的方式获取日志列表
lock (_logCache)
{
return _logCache.ToList();
var logs = _logCache.ToList();
_logger.LogDebug("GetLogCache: 返回 {Count} 条日志", logs.Count);
return logs;
}
}
/// <summary>
/// 获取当前缓存的日志总数
/// </summary>
public int GetLogCacheCount() => _logCache.Count;
public int GetLogCacheCount()
{
var count = _logCache.Count;
_logger.LogDebug("GetLogCacheCount: 当前缓存 {Count} 条日志", count);
return count;
}
/// <summary>
/// 清空日志缓存
@ -576,6 +583,33 @@ namespace LTEMvcApp.Services
}
}
/// <summary>
/// 获取日志缓存详细状态(调试用)
/// </summary>
/// <returns>缓存状态信息</returns>
public object GetLogCacheStatus()
{
lock (_logCache)
{
var logs = _logCache.ToList();
var sampleLogs = logs.TakeLast(3).Select(log => new
{
timestamp = log.Timestamp,
layer = log.Layer,
message = log.Message?.Substring(0, Math.Min(50, log.Message?.Length ?? 0)) + "..."
}).ToList();
return new
{
totalCount = _logCache.Count,
actualCount = logs.Count,
cacheSize = LogCacheSize,
sampleLogs = sampleLogs,
timestamp = DateTime.UtcNow
};
}
}
#endregion
#region 私有方法

402
LTEMvcApp/test_client_config.json

@ -8,7 +8,7 @@
"logs": {
"layers": {
"PHY": {
"level": "warn",
"level": "info",
"maxSize": 1000,
"payload": true,
"filter": "warn",
@ -106,28 +106,6 @@
"debug": null,
"max": null
},
"GTPC": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"IP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"X2AP": {
"level": "debug",
"maxSize": 1000,
@ -160,384 +138,10 @@
"epc": false,
"debug": null,
"max": null
},
"IMS": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"CX": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"RX": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"COM": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"SIP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"MEDIA": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"RTP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"PROD": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"S6": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"S13": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"SGsAP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"SBcAP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N8": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N12": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N13": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N17": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N50": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"MMS": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"HTTP2": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"LCSAP": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"LPPa": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"NL1": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"NRPPa": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"IKEV2": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"SWU": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"NWU": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"IPSEC": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"N3IWF": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"TRX": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"MON": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"ALARM": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"LIC": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"OTS": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
},
"ERROR": {
"level": "warn",
"maxSize": 1000,
"payload": false,
"filter": "warn",
"color": "#000000",
"direction": {},
"epc": false,
"debug": null,
"max": null
}
},
"signal": true,
"cch": true,
"signal": null,
"cch": null,
"extensionData": null
},
"pause": false,

Loading…
Cancel
Save