IIS監控應用程序池和站點假死,自動重啟IIS小工具


文章技術適合初學者。高級的C#開發工程師這些估計都熟悉到爛了,望不要噴。

第一、C#代碼要操作IIS 就必須先導入 Microsoft.Web.Administration.dll ,方便控制台程序做成windows服務,還要導入Topshelf.dll,附件中有這兩個dll,

想要玩一下的可以下載試試,點擊Install.bat做windows服務,也可以直接點擊exe文件在控制台上查看您要的效果,  點擊下載附件.

第二、整個小工具就兩個類,Program.cs , IISWatcherControl.cs 直接貼代碼了,這個小工具只是為了幫您自動重啟IIS,但是程序為什么會崩潰或者 程序池會掛掉,

還是要您自己檢查下寫的代碼哪里寫的不合理導致的.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace IISWatcherService
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run((x) =>
            {
                x.Service<IISWatcherControl>();
                x.RunAsLocalSystem();
                x.SetServiceName("IISWatcherService");
                x.SetDisplayName("IISWatcherService");
                x.SetDescription("監控IIS運行狀態");
            });
        }
    }
}

C#監控IIS代碼

using System;
using System.Collections.Generic; 
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Web.Administration;
using Topshelf;

namespace IISWatcherService
{
    public class IISWatcherControl : ServiceControl
    {

        #region ServiceControl 成員

        public bool Start(HostControl hostControl)
        {
            MonitoringIISApp();
            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            return true;
        }
     /// <summary>     
        /// 每個十秒鍾監控一次是否有暫停的web站點和應用程序池     
        /// </summary>
        public void MonitoringIISApp()
        {
            ServerManager webIIS = new ServerManager();
            Task.Factory.StartNew(() =>
            {
                var result = string.Empty;
                while (true)
                {
                   //獲取IIS站點
                    var sites = webIIS.Sites;                    
                    foreach (var item in sites)
                    {
                        if (item.Bindings.Any(ii => ii.Protocol != "ftp") && item.State == ObjectState.Stopped)
                        {
                            if (item.Start() == ObjectState.Started)
                            {
                                result = string.Format(item.Name + ",站點啟動成功 {0}",DateTime.Now);
                                Console.WriteLine(result);
                                WriteFile(result);
                            }
                        }
                    }
            //獲取應用程序池
                    var applications = webIIS.ApplicationPools;
                    foreach (var item in applications)
                    {
                        if (item.State == ObjectState.Stopped)
                        {
                            if (item.Start() == ObjectState.Started)
                            {
                                result = string.Format(item.Name + ",應用程序池開啟成功 {0}", DateTime.Now);
                                Console.WriteLine(result);
                                WriteFile(result);
                            }
                        }
                    }
                    Thread.Sleep(TimeSpan.FromSeconds(10d));
                }
            });
        }    

      /// <summary>
      /// 日志寫入文件
      /// </summary>

                    private void WriteFile(string message)       

      {

            
            var directorypath = AppDomain.CurrentDomain.BaseDirectory + @"\LogFile";
            if (!Directory.Exists(directorypath))
            {
                Directory.CreateDirectory(directorypath);
            }
            var path = string.Format(directorypath + @"\log_{0}.txt", DateTime.Now.ToString("yyyyMMdd"));
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            using (StreamWriter sw=new StreamWriter(path,true,System.Text.Encoding.UTF8))
            {
                sw.WriteLine(message);
            }
        }

        #endregion
    }
}

 時間飛快2017年一下就過去了,這是2018年的第一篇文章,希望今年可以寫些博文。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM