c#法式按期把內存信息記載到log日記示例。本站提示廣大學習愛好者:(c#法式按期把內存信息記載到log日記示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#法式按期把內存信息記載到log日記示例正文
設立一個准時器tmrMonitor,該准時器會在法式運轉時赓續把法式的占用內存和占用線程數寫到LOG\MEM目次下。
我設置的准時器距離是3000毫秒,記載後的信息可以用來剖析一段時光內法式的運轉狀態,好比內存洩露成績。
/// <summary>
/// Timer組件tmrMonitor的Tick事宜
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tmrMonitor_Tick(object sender, EventArgs e)
{
string LogAddress = Environment.CurrentDirectory + "\\Log";
if (!Directory.Exists(LogAddress + "\\MEM")) //須要System.IO
{
Directory.CreateDirectory(LogAddress + "\\MEM");
}
LogAddress = String.Concat(LogAddress, "\\MEM\\",
DateTime.Now.Year, '-', DateTime.Now.Month, '-',
DateTime.Now.Day, "_mem.log");
//須要 System.Diagnostics;
Process currentProcess = Process.GetCurrentProcess();
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine('[' + DateTime.Now.ToString() + ']');
sw.WriteLine("過程標識: " + currentProcess.Id.ToString());
sw.WriteLine("過程稱號: " + currentProcess.ProcessName.ToString());
sw.WriteLine("占用內存: " +
(currentProcess.WorkingSet64 / 1024).ToString() + "KB");
sw.WriteLine("線程數目: " + currentProcess.Threads.Count.ToString());
sw.WriteLine();
sw.Close();
}