1. 程式人生 > >應用程式池監控

應用程式池監控

 

應用程式池半夜掛掉,寫個程式監控重啟

string entPath = "IIS://LOCALHOST/W3SVC/AppPools"; 
StringBuilder sbLog = new StringBuilder();
while (true)
{
try
{
DirectoryEntry rootEntry = new DirectoryEntry(entPath);
foreach (DirectoryEntry AppPool in rootEntry.Children)
{
if (AppPool.Properties["AppPoolState"].Value.ToString() != "2")
{
sbLog.AppendFormat("{0} {1} \r\n", DateTime.Now.ToString("mm-ss-ffff"), "監控到" + AppPool.Name + "應用地址池停止.");
Console.Write(DateTime.Now.ToString() + "監控到" + AppPool.Name + "應用地址池停止.\n");
AppPool.Invoke("Start", null);
AppPool.CommitChanges();
Console.Write("應用地址池已成功啟動.\n");
Log.WriteLog("IIS", sbLog.ToString());
}
AppPool.Close();
}
}
catch(Exception ex)
{
Log.WriteLog("IIS", ex.Message);
}
Thread.Sleep(60000);
}