zoukankan
html css js c++ java
一个系统日志EventLog的示例(downmoon)
原来是发在CSDN论坛的,索性拿出来给更多的人分享
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Diagnostics;
using
System.ServiceProcess;
using
System.Net;
using
System.Net.Sockets;
namespace
wsPing
{
public
class
pingService : System.ServiceProcess.ServiceBase
{
public
System.Diagnostics.EventLog evLog;
private
System.Timers.Timer TimerPing;
/**/
/**/
/**/
///
<summary>
///
必需的设计器变量。
///
</summary>
private
System.ComponentModel.Container components
=
null
;
//
ping 一个页面地址*********************************
int
iPingInterval
=
180000
;
//
3分钟
string
sPingAddress
=
"
www.buynow.com.cn
"
;
//
**************************************************
public
pingService()
{
//
该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent();
//
TODO: 在 InitComponent 调用后添加任何初始化
//
如果不存在日志************************************
if
(
!
System.Diagnostics.EventLog.SourceExists(
"
logService
"
))
{
EventLog.CreateEventSource(
"
logService
"
,
"
logServiceLog
"
);
}
this
.evLog.Source
=
"
logService
"
;
//
如果要重新命名,必须重新启动计算机
//
this.evLog.Log="logServiceLog";
//
**************************************************
}
//
进程的主入口点
static
void
Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
//
同一进程中可以运行多个用户服务。若要将
//
另一个服务添加到此进程,请更改下行
//
以创建另一个服务对象。例如,
//
//
ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun
=
new
System.ServiceProcess.ServiceBase[]
{
new
pingService() }
;
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/**/
/**/
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器
///
修改此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.evLog
=
new
System.Diagnostics.EventLog();
this
.TimerPing
=
new
System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(
this
.evLog)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.TimerPing)).BeginInit();
//
//
TimerPing
//
this
.TimerPing.Interval
=
60000
;
this
.TimerPing.Elapsed
+=
new
System.Timers.ElapsedEventHandler(
this
.TimerPing_Elapsed);
//
//
pingService
//
this
.CanHandlePowerEvent
=
true
;
this
.CanPauseAndContinue
=
true
;
this
.CanShutdown
=
true
;
this
.ServiceName
=
"
pingService
"
;
((System.ComponentModel.ISupportInitialize)(
this
.evLog)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.TimerPing)).EndInit();
}
/**/
/**/
/**/
///
<summary>
///
清理所有正在使用的资源。
///
</summary>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
/**/
/**/
/**/
///
<summary>
///
设置具体的操作,以便服务可以执行它的工作。
///
</summary>
protected
override
void
OnStart(
string
[] args)
{
//
TODO: 在此处添加代码以启动服务。
this
.evLog.WriteEntry(
"
pingService is Starting……………………………
"
);
TimerPing.Interval
=
this
.iPingInterval;
this
.TimerPing.Enabled
=
true
;
}
/**/
/**/
/**/
///
<summary>
///
停止此服务。
///
</summary>
protected
override
void
OnStop()
{
//
TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this
.evLog.WriteEntry(
"
pingService is Stopping……………………………
"
);
this
.TimerPing.Enabled
=
false
;
}
/**/
/**/
/**/
///
<summary>
///
暂停
///
</summary>
protected
override
void
OnPause()
{
this
.evLog.WriteEntry(
"
pingService is Pausing……………………………
"
);
}
/**/
/**/
/**/
///
<summary>
///
继续
///
</summary>
protected
override
void
OnContinue()
{
this
.evLog.WriteEntry(
"
pingService is Continuing……………………………
"
);
}
private
void
TimerPing_Elapsed(
object
sender, System.Timers.ElapsedEventArgs e)
{
Pinger pi
=
new
Pinger();
if
(pi.Ping(
this
.sPingAddress)
<
1
)
{
evLog.WriteEntry(sPingAddress
+
"
does not respond.
"
);
}
}
}
public
class
Pinger
{
public
int
Ping (
string
addr)
{
Socket sck
=
new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
try
{
System.Net.IPHostEntry ipInfo
=
System.Net.Dns.Resolve(addr);
IPEndPoint ipe
=
new
IPEndPoint(ipInfo.AddressList[
0
],
8
);
sck.Connect(ipe);
}
catch
{
return
-
1
;
}
return
1
;
}
}
}
邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。
助人等于自助!
3w@live.cn
查看全文
相关阅读:
部署tomcat应用的三种方法
【转】Linux下如何查看CPU信息, 包括位数和多核信息
Tomcat端口占用的处理方式
jconsole tomcat内存监控设置
[转]成功的 Web 应用系统性能测试
[转]Xmanager连接Linux远程桌面(后面添加了自己的部分)
【转】配置远程jconsole监测tomcat
大同小忆记五一大同之行
理解Tomcat的WebappClassLoader(web应用类加载器)
如何用PS批量为照片添加图片和文字水印
原文地址:https://www.cnblogs.com/downmoon/p/1279154.html
最新文章
SQL The data types ntext and varchar are incompatible in the not equal to operator 错误
判断数据库、数据表是否存在
C#配置文件App.config
简单工厂模式
判断windows是否装有某软件
给tomcat添加管理用户,修改tomcat访问端口
tomcat正常启动,在服务器本地可以访问,在服务器以外浏览器不能访问
DataGridView导出为EXCEL
委托和事件
Sort 排序
热门文章
截图
css模拟小气泡三角(二)
用纯css模拟小气泡三角
ie6,7 SCRIPT1028: 缺少标识符、字符串或数字
关闭按钮的小叉
ie6经典bug弹出框盖不住select的解决办法
html email(EDM)制作总结
【转】Tomcat 的日志
ubuntu下nginx安装
windows下JMX连接Tomcat
Copyright © 2011-2022 走看看