码迷,mamicode.com
首页 > Windows程序 > 详细

C#后台程序重启IIS,发邮件通知

时间:2015-10-02 12:29:01      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人;

主要代码:

监控类

技术分享
public class monitoringiis
    {
        EmailSend send = new EmailSend();
        public void MonitoringISSAppPool(object obj)
        {
            string method_Start = "Start";
            string ISSAppPoolName = "";
            string iislist = System.Configuration.ConfigurationManager.AppSettings["iislist"];
            DirectoryEntry appPool = new DirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools");
            while (true)
            {
                try
                {
                    foreach (DirectoryEntry item in appPool.Children)
                    {
                        string AppPoolCommand = item.Properties["ManagedPipelineMode"].Value.ToString();
                        string ManagedRuntimeVersion = item.Properties["ManagedRuntimeVersion"].Value.ToString();//,net版本号                    
                        string AppPoolState = item.Properties["AppPoolState"].Value.ToString();//当前状态                    
                        if (AppPoolState != "2" && iislist.Contains(item.Name.Trim()))
                        {
                            DirectoryEntry findPool = appPool.Children.Find(item.Name, "IIsApplicationPool");
                            findPool.Invoke(method_Start, null);
                            ISSAppPoolName = item.Name;
                            appPool.CommitChanges();
                            appPool.Close();
                            var IIsMsg = string.Format("应用池名称:[{0}],NET版本:{1},标识:{2},当前状态:[{3}],时间:{4}/n", item.Name, ManagedRuntimeVersion,
                                item.SchemaClassName, "已启动", DateTime.Now.ToString());
                            logclass.Info(IIsMsg);
                        }
                    }

                    Thread.Sleep(180000);

                }
                catch (Exception ex)
                {

                    logclass.Error("MonitoringISSAppPool:" + ISSAppPoolName + string.Format("==时间:{0},启动失败", DateTime.Now) + "==" + ex.Source + "==" + ex.InnerException + "==" + ex.TargetSite + "==" + ex.Message);//日志记录错误信息              
                    send.SendMail(string.Format("服务器:" + GetComputerInfo() + "==MonitoringISSAppPool:{0},时间:{1},启动失败", ISSAppPoolName, DateTime.Now) + "==" + ex.Message,
                        ISSAppPoolName + "ISS服务发生问题,请尽快查看原因并解决");//邮件发送错误信息    
                    Thread.Sleep(10000);
                }
            }
        }

        /// <summary>获取当前计算机信息   例如:计算机名:aaa,IP:10.10.10.10</summary>        
        /// <returns></returns>        
        public static string GetComputerInfo()
        {
            var computerNmae = System.Net.Dns.GetHostName();//获取当前计算机名称            
            var computerIp = new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString();
            //获取局域网IP                             
            return string.Format("PC_Name: {0},IP: {1}", computerNmae, computerIp);
        }
    }
View Code

发邮件类:

技术分享
public class EmailSend
    {
        public void SendMail(string sendmessage,string sendtitle)
        {
            string senduser = System.Configuration.ConfigurationManager.AppSettings["senduser"];
            string sendpwd = System.Configuration.ConfigurationManager.AppSettings["sendpwd"];
            string[] receiveusers = System.Configuration.ConfigurationManager.AppSettings["receiveusers"].Split(|);
            string[] fasong = senduser.Split(@);
            string[] fs = fasong[1].Split(.);

            //发送
            SmtpClient client = new SmtpClient("smtp.163.com");   //设置邮件协议
            client.UseDefaultCredentials = false;//这一句得写前面
            client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器
            client.Credentials = new NetworkCredential(fasong[0].ToString(), sendpwd); //通过用户名和密码 认证
            foreach (string jsuser in receiveusers)
            {
                MailMessage mmsg = new MailMessage(new MailAddress(senduser), new MailAddress(jsuser)); //发件人和收件人的邮箱地址
                mmsg.Subject = sendtitle;      //邮件主题
                mmsg.SubjectEncoding = Encoding.UTF8;   //主题编码
                mmsg.Body = sendmessage;         //邮件正文
                mmsg.BodyEncoding = Encoding.UTF8;      //正文编码
                mmsg.IsBodyHtml = true;    //设置为HTML格式           
                mmsg.Priority = MailPriority.High;   //优先级
                try
                {
                    client.Send(mmsg);
                }
                catch (Exception ex)
                {
                    logclass.Debug("发送邮件失败:" + ex.Message);
                }
            }
        }
    }
View Code

 

C#后台程序重启IIS,发邮件通知

标签:

原文地址:http://www.cnblogs.com/systemkk/p/4852051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!