2508271256
HeartbeatSender添加Try-Catch與Writelog
This commit is contained in:
parent
5ddd093273
commit
2466c8cb6a
@ -18,7 +18,7 @@ public class heartbeatSender
|
||||
private string branchName = "";
|
||||
private string heartbeatDomain = "";
|
||||
private CancellationTokenSource? cancellationTokenSource;
|
||||
public static Dictionary<string, string> DbData= new Dictionary<string, string>() ;
|
||||
public static Dictionary<string, string> DbData = new Dictionary<string, string>();
|
||||
public heartbeatSender()
|
||||
{
|
||||
this.heartbeatDomain = Utils.Env.Get("HeartBeatUrl", "");
|
||||
@ -51,18 +51,30 @@ public class heartbeatSender
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"心跳任務錯誤:{ex.Message}");
|
||||
WriteLog(ex.Message);
|
||||
await Task.Delay(5000); // 錯誤後延遲再跑
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
try
|
||||
{
|
||||
cancellationTokenSource?.Cancel();
|
||||
Console.WriteLine("心跳任務已停止");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"心跳任務錯誤:{ex.Message}");
|
||||
WriteLog(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<bool> LoginAndGetTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{heartbeatDomain.TrimEnd('/')}/api/room/receiveRegister";
|
||||
var loginPayload = new
|
||||
@ -92,8 +104,18 @@ public class heartbeatSender
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return default;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task SendHeartbeatAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{heartbeatDomain.TrimEnd('/')}/api/room/heartbeat";
|
||||
string hostName = Dns.GetHostName();
|
||||
@ -111,8 +133,18 @@ public class heartbeatSender
|
||||
await SendPostAsync<JsonElement>(url, heartbeatPayload, token);
|
||||
Console.WriteLine($"心跳送出成功: {DateTime.Now:HH:mm:ss}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async Task<T?> SendPostAsync<T>(string url, object payload, string? bearerToken = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
@ -133,8 +165,19 @@ public class heartbeatSender
|
||||
string responseJson = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<T>(responseJson);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return default(T);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static string GetLocalIPv4()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
|
||||
{
|
||||
@ -143,24 +186,85 @@ public class heartbeatSender
|
||||
}
|
||||
return "127.0.0.1";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private float GetCpuUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
|
||||
cpuCounter.NextValue();
|
||||
Thread.Sleep(100);
|
||||
return cpuCounter.NextValue();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return float.NaN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private float GetTotalMemoryInMB()
|
||||
{
|
||||
try
|
||||
{
|
||||
var ci = new ComputerInfo();
|
||||
return (ci.TotalPhysicalMemory - ci.AvailablePhysicalMemory) / 1024f;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return float.NaN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private float GetDiskTotalSizeInGB(string driveLetter = "C")
|
||||
{
|
||||
try
|
||||
{
|
||||
var drive = new DriveInfo(driveLetter);
|
||||
return drive.TotalSize / (1024f * 1024f * 1024f);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
WriteLog(ex.Message);
|
||||
return float.NaN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void WriteLog(string message)
|
||||
{
|
||||
// 指定日志文件的路径
|
||||
string logFilePath = Path.Combine(Application.StartupPath, "txt", "mainlog.txt");
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 使用 StreamWriter 来向日志文件追加文本
|
||||
using (StreamWriter writer = new StreamWriter(logFilePath, true))
|
||||
{
|
||||
writer.WriteLine(String.Format("[{0}] {1}", DateTime.Now, message));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果写入日志文件时发生错误,这里可以处理这些异常
|
||||
// 例如:打印到控制台
|
||||
Console.WriteLine(String.Format("Error writing to log file: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user