/// IIS应用地址池监控方法
/// </summary>
private void IISAppPools()
{
try
{
string entPath = "IIS://LOCALHOST/W3SVC/AppPools";
while (true)
{
DirectoryEntry rootEntry = new DirectoryEntry(entPath);
foreach (DirectoryEntry AppPool in rootEntry.Children)
{
if (AppPool.Properties["AppPoolState"].Value.ToString() != "2")
{
LogHelper.WriteLog("监控到" + AppPool.Name + "应用地址池停止.");
AppPool.Invoke("Start", null);
AppPool.CommitChanges();
LogHelper.WriteLog(AppPool.Name + "应用地址池已成功启动.");
}
AppPool.Close();
}
Thread.Sleep(600000);
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.ToString());
}
}
public class LogHelper
{
public static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void WriteLog(string str)
{
string param = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
logger.Error(str);
}
}
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
WebClient webClient = new WebClient();
// 要提交表单的URI字符串。
string uriString = txtUrl.Text.Trim();
// 要提交的字符串数据。格式形如:user=uesr1&password=123
string postString = txtPostStr.Text.Trim();
// 初始化WebClient
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
byte[] responseData = webClient.UploadData(uriString, "POST", postData);
// 将返回的将字节数组转换成字符串(HTML);
// ASP.NET 返回的页面一般是Unicode,如果是简体中文应使用
// Encoding.GetEncoding("GB2312").GetString(responseData)
string srcString = Encoding.UTF8.GetString(responseData);
richTextBox1.AppendText(srcString + "\r\n");
richTextBox1.ScrollToCaret();
string uri = txtMainpage.Text;
//string uri = txtUrl.Text;
Stream st = webClient.OpenRead(uri);
StreamReader sr = new StreamReader(st);
string res = sr.ReadToEnd();
sr.Close();
st.Close();
if (res.Contains(Utility.GetConfigValue("ServerError")) || res.Contains(Utility.GetConfigValue("ServiceUnavailable")))
{
IISAppPools();
}
=====================================================================================
收集资料:
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC");
System.Collections.IEnumerator enumerator = appPool.Children.GetEnumerator();
DirectoryEntry subItem;
while (enumerator.MoveNext())
{
subItem = (DirectoryEntry) enumerator.Current;
if (subItem.SchemaClassName == "IIsWebServer")
{
int intServerState = Convert.ToInt32(subItem.Properties["ServerState"].Value);
switch (intServerState)
{
case 1: //Starting
case 2: //Started
case 3: //Stopping
case 4: //Stopped
case 5: //Pausing
case 6: //Paused
case 7: //Continuing
default://Unknown
}
}
}
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry findPool =appPool.Children.Find(AppPoolName,"IIsApplicationPool");
findPool.Invoke(method,null);
appPool.CommitChanges();
appPool.Close();
用下面代码可以检查一个服务是否被启动
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = AppPoolName;//"MSSQLSERVER";
if( sc.Status != ServiceControllerStatus.Running )
sc.Start();