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 ...
随机推荐
- 23-THREE.JS 光照材质
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- 内存保护机制及绕过方案——从堆中绕过safeSEH
1.1 SafeSEH内存保护机制 1.1.1 Windows异常处理机制 Windows中主要两种异常处理机制,Windows异常处理(VEH.SEH)和C++异常处理.Windows异 ...
- CSS:Tutorial four
1.CSS Tables To specify table borders in CSS, use the border property. The example below specifies a ...
- 原创:Angular新手容易碰到的坑,随时更新,欢迎订阅
在Angular群里回答新手问题一段时间了,有一些Angular方面的坑留在这里备查,希望能对各位有所帮助.这个文章将来会随时更新,不会单独开新章,欢迎各位订阅. Q1. <div ng-inc ...
- 剑指offer--35.数组中只出现一次的数字
时间限制:1秒 空间限制:32768K 热度指数:198150 本题知识点: 数组 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. class ...
- Report: Disappearing Wetlands Put Planet Life at Risk
A new report warns that wetlands are disappearing three times faster than the world’s forests, with ...
- flash播放器插件与flash播放器的区别
flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.
- Android application testing with the Android test framework
目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...
- Juint 单元测试(2)
单元测试(junit testing),是指对软件中的最小可测试单元进行检查和验证.Java里单元指一个类. JUnit ,是一个开源的Java单元测试框架,是 Java的标准单元测试库,是非常重要第 ...
- Ubuntu 安装文本编译器notepad++,ultraEdit
Windows下用过的文本编辑器主要有notepad++和UltraEdit,这里记录一下这两种编辑器在Ubuntu下的安装方法: Notepad++: 其实Ubuntu下无法按安装Notepad++ ...