IIS应用地址池监控方法

   // <summary>
        /// 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();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值