目前我正在使用下面的1个代码,它工作得非常好。
我正在获取服务器的 CPU 和内存利用率,并在循环 10-15 秒后发送邮件。此循环运行 5 次,间隔为 10 秒。
它正在发送 5 封邮件。
我不想发送 5 封不同的邮件,而只想发送 1 封邮件。彼此下方有 5 行。
for (int i = 0; i < 5; i++)
{
float cpu = getCPUCOunter();
float memory = getRamCounter();
ulong TotalMemory = new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
TotalMemory = TotalMemory / 1024000;
float PhysicalMemory = (TotalMemory - memory) * 100 / TotalMemory;
SendMail("PROD Server CPU and Physical Memory Utalization",
"CPU Utilization = " + Math.Round(cpu) + "%\r\n"
+ Environment.NewLine + " \nPhysical Memory Utilization " +
Math.Round(PhysicalMemory) + "%" + Environment.NewLine +
" at " + DateTime.Now.ToString("HH:mm:ss tt"));
Thread.Sleep(10000);
}
当前输出(样本):
Mail 1 : CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
Mail 2 : CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
Mail 3 : CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
Mail 4 : ----= ----
Mail 5 : --- = -----
预期输出(一封邮件中的所有上述行):
CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM
CPU Utilization = 0% Physical Memory Utilization 61% at 14:06:32 PM