增加日志组件
This commit is contained in:
@@ -6,13 +6,32 @@ public class ServiceConfig
|
||||
// 1. 基础属性
|
||||
// ==========================================
|
||||
public int ParentPid { get; private set; }
|
||||
|
||||
public string AppId { get; private set; } = "Unknown_01";
|
||||
|
||||
public int NumericId { get; private set; } = 1;
|
||||
|
||||
public int BasePort { get; private set; } = 5000;
|
||||
|
||||
public int MaxPortRange { get; private set; } = 100;
|
||||
|
||||
public NetworkMode Mode { get; private set; } = NetworkMode.Passive;
|
||||
|
||||
public bool ShouldConnect => Mode == NetworkMode.Active || Mode == NetworkMode.Hybrid;
|
||||
|
||||
public string SeqServerUrl { get; private set; } = string.Empty;
|
||||
|
||||
public string SeqApiKey { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 更新实际端口
|
||||
/// </summary>
|
||||
/// <param name="port"></param>
|
||||
public void UpdateActualPort(int port)
|
||||
{
|
||||
this.BasePort = port;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 2. 目标地址列表 (类型变了!)
|
||||
// ==========================================
|
||||
@@ -31,12 +50,16 @@ public class ServiceConfig
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
var key = args[i].ToLower().Trim();
|
||||
|
||||
// 确保不越界且下一个参数不是 key (防止 value 为空的情况)
|
||||
var value = (i + 1 < args.Length && !args[i + 1].StartsWith("--")) ? args[i + 1] : string.Empty;
|
||||
bool consumed = !string.IsNullOrEmpty(value);
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case "--pid": if (int.TryParse(value, out int pid)) config.ParentPid = pid; break;
|
||||
case "--pid":
|
||||
if (int.TryParse(value, out int pid)) config.ParentPid = pid;
|
||||
break;
|
||||
case "--appid":
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
@@ -49,12 +72,14 @@ public class ServiceConfig
|
||||
break;
|
||||
case "--mode": if (int.TryParse(value, out int m)) config.Mode = (NetworkMode)m; break;
|
||||
case "--ports":
|
||||
if (!string.IsNullOrWhiteSpace(value) && value.Contains(","))
|
||||
{
|
||||
var parts = value.Split(',');
|
||||
if (parts.Length >= 1 && int.TryParse(parts[0], out int p)) config.BasePort = p;
|
||||
if (parts.Length >= 2 && int.TryParse(parts[1], out int r)) config.MaxPortRange = r;
|
||||
}
|
||||
ParsePortConfig(config, value);
|
||||
break;
|
||||
case "--sequris":
|
||||
config.SeqServerUrl = ParseSeqUri(value);
|
||||
break;
|
||||
case "--seqkey":
|
||||
// 去掉可能存在的分号
|
||||
config.SeqApiKey = value.Replace(";", "").Trim();
|
||||
break;
|
||||
}
|
||||
if (consumed) i++;
|
||||
@@ -62,6 +87,54 @@ public class ServiceConfig
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析端口配置
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="value"></param>
|
||||
private static void ParsePortConfig(ServiceConfig config, string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value) && value.Contains(","))
|
||||
{
|
||||
var parts = value.Split(',');
|
||||
if (parts.Length >= 1 && int.TryParse(parts[0], out int p)) config.BasePort = p;
|
||||
if (parts.Length >= 2 && int.TryParse(parts[1], out int r)) config.MaxPortRange = r;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析 Seq URI
|
||||
/// </summary>
|
||||
/// <param name="rawValue"></param>
|
||||
/// <returns></returns>
|
||||
private static string ParseSeqUri(string rawValue)
|
||||
{
|
||||
// 格式: 172.16.41.241,20026,日志处置中心;
|
||||
try
|
||||
{
|
||||
rawValue = rawValue.Replace("\"", "").TrimEnd(';'); // 清理引号和末尾分号
|
||||
var parts = rawValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (parts.Length >= 2)
|
||||
{
|
||||
string ip = parts[0].Trim();
|
||||
string port = parts[1].Trim();
|
||||
// 组装成标准 HTTP 格式
|
||||
return $"http://{ip}:{port}";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 AppId 中解析 ID
|
||||
/// </summary>
|
||||
/// <param name="appId"></param>
|
||||
/// <returns></returns>
|
||||
private static int ParseIdFromAppId(string appId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(appId)) return 1;
|
||||
|
||||
Reference in New Issue
Block a user