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年的第一篇文章,希望今年可以写些博文。
IIS监控应用程序池和站点假死,自动重启IIS小工具的更多相关文章
- Tomcat假死的原因及解决方案
服务器配置:linux+tomcat 现象:Linux服务器没有崩,有浏览器中访问页面,出现无法访问的情况,没有报4xx或5xx错误(假死),并且重启tomcat后,恢复正常. 原因:tomcat默认 ...
- C#后台程序重启IIS,发邮件通知
应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...
- 自动清理IIS log 日志脚本
系统环境:windows server 2012 r2 IIS 版本:IIS8 操作实现清理IIS log File 脚本如下: @echo off ::自动清理IIS Log file set lo ...
- iis应用程序池假死问题
“Comprehensive orientate 16:05:43 查看原文 IIS貌似问题不少 问:IIS 网站 并发连接线不多,但是运行一段时间后 就非常慢,系统资源占用都正常,一回收应用 ...
- C#操作IIS程序池及站点的创建配置
最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...
- IIS7.5解决应用程序池回收假死问题
使用windows server 2008 r2解决回收假死的问题. 具体做法是: 打开应用程序池 -> 高级设置 ->在“禁止重叠回收”里选择“true”,这样就有效避免了应用程序池回收 ...
- IIS7.5 用 IIS AppPool\应用程序池名 做账号 将各站点权限分开
IIS6里面,要把服务器上的各站点权限分开,要建一堆帐号,再一个一个站点绑定.IIS7.5就不用了. 选择 "应用程序用户" 选择 "应用程序用户",启动应用程 ...
- C#操作IIS程序池及站点的创建配置(转)
原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...
- C#操作IIS程序池及站点的创建配置实现代码
首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...
随机推荐
- Redis-sentinel哨兵模式集群方案配置
一.sentinel介绍 Redis Sentinel Sentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中 Sentinel作用: 1) ...
- 【spark】连接Hbase
0.我们有这样一个表,表名为Student 1.在Hbase中创建一个表 表明为student,列族为info 2.插入数据 我们这里采用put来插入数据 格式如下 put ‘表命’,‘行键’, ...
- samba配置只读和可以写入的共享
编辑smb.conf 1.在[global]中 找到 security = 将其改为 security = share 2. 在文件中加入自定义的共享目录 [attachment] path=/dat ...
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【git】不同协议的路径
https https://github.com/yesuuu/ganLearn.git ssh git@github.com:yesuuu/ganLearn.git git@idc:ganLearn ...
- NTP服务器和国内可用的NTP地址
NTP 是什么? NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备[如计算机.手机]的时间的协议. NTP 实现什么目的? 目的很简单,就是为了提供准确 ...
- python学习之控制语句
#if statement number=int(input("please input a number")); if number<10 : print("is ...
- 【JD的一人戏】之"小羊踢足球"第一篇
好多次加班后坐的士回家,副驾驶座位后内嵌的显示屏正好在播放一个美食节目,内容就是一个着装怪异的厨子把各种食材分门别类地摆在你面前,然后用小小的锅碗瓢盆慢慢的做出很精致的够一个人吃的分量的各种美食,做好 ...
- [python] 获得所有的最长公共子序列
两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...
- Python基本特殊方法之__new__
__new__()和不可变对象 __new__方法的一个用途是初始化不可变对象,__new()__方法中允许创建未初始化的对象,这允许我们在__init__()方法被调用之前先设置对象的属性 例:为f ...