0

目前我正在使用下面的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 
4

2 回答 2

2

只需将消息附加到字符串并在最后发送所有内容:

string msg = string.empty
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;

     msg += "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") + Environment.Newline;
     Thread.Sleep(10000);
}

SendMail("PROD Server CPU and Physical Memory Utalization", msg)
于 2013-05-09T08:45:23.233 回答
0

捕获数组中的值,并将“SendMail”方法调用移出 for 循环语句。

在此 for 循环之后创建另一个 for 循环,以将存储在数组中的值显示为行数。

于 2013-05-09T08:50:29.947 回答