一个系统日志EventLog的示例(downmoon)

原来是发在CSDN论坛的,索性拿出来给更多的人分享

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Diagnostics;
None.gif
using  System.ServiceProcess;
None.gif
using  System.Net;
None.gif
using  System.Net.Sockets;
None.gif
None.gif
None.gif
namespace  wsPing
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class pingService : System.ServiceProcess.ServiceBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public System.Diagnostics.EventLog evLog;
InBlock.gif        
private System.Timers.Timer TimerPing;
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary> 
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary> 

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
//ping 一个页面地址*********************************
InBlock.gif
        int iPingInterval =180000;//3分钟
InBlock.gif
        string sPingAddress="www.buynow.com.cn";
InBlock.gif        
//**************************************************
InBlock.gif

InBlock.gif
InBlock.gif        
public pingService()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 该调用是 Windows.Forms 组件设计器所必需的。
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
// TODO: 在 InitComponent 调用后添加任何初始化
InBlock.gif
InBlock.gif            
//如果不存在日志************************************
InBlock.gif
            if(!System.Diagnostics.EventLog.SourceExists("logService"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EventLog.CreateEventSource(
"logService","logServiceLog");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.evLog.Source="logService";
InBlock.gif            
//如果要重新命名,必须重新启动计算机
InBlock.gif            
//this.evLog.Log="logServiceLog";
InBlock.gif            
//**************************************************
InBlock.gif

InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 进程的主入口点
InBlock.gif
        static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.ServiceProcess.ServiceBase[] ServicesToRun;
InBlock.gif    
InBlock.gif            
// 同一进程中可以运行多个用户服务。若要将
InBlock.gif            
//另一个服务添加到此进程,请更改下行
InBlock.gif            
// 以创建另一个服务对象。例如,
InBlock.gif            
//
InBlock.gif            
//   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
InBlock.gif            
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
            ServicesToRun = new System.ServiceProcess.ServiceBase[] dot.gifnew pingService() };
InBlock.gif
InBlock.gif            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary> 
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器 
InBlock.gif        
/// 修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary> 

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.evLog = new System.Diagnostics.EventLog();
InBlock.gif            
this.TimerPing = new System.Timers.Timer();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.evLog)).BeginInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.TimerPing)).BeginInit();
InBlock.gif            
// 
InBlock.gif            
// TimerPing
InBlock.gif            
// 
InBlock.gif
            this.TimerPing.Interval = 60000;
InBlock.gif            
this.TimerPing.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerPing_Elapsed);
InBlock.gif            
// 
InBlock.gif            
// pingService
InBlock.gif            
// 
InBlock.gif
            this.CanHandlePowerEvent = true;
InBlock.gif            
this.CanPauseAndContinue = true;
InBlock.gif            
this.CanShutdown = true;
InBlock.gif            
this.ServiceName = "pingService";
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.evLog)).EndInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.TimerPing)).EndInit();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 设置具体的操作,以便服务可以执行它的工作。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void OnStart(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO: 在此处添加代码以启动服务。
InBlock.gif
            this.evLog.WriteEntry("pingService is Starting……………………………");
InBlock.gif            TimerPing.Interval
=this.iPingInterval;
InBlock.gif            
this.TimerPing.Enabled=true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 停止此服务。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void OnStop()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
InBlock.gif
            this.evLog.WriteEntry("pingService is Stopping……………………………");
InBlock.gif            
this.TimerPing.Enabled=false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 暂停
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void OnPause()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.evLog.WriteEntry("pingService is Pausing……………………………");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
///继续
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void OnContinue()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.evLog.WriteEntry("pingService is Continuing……………………………");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void TimerPing_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Pinger pi
=new Pinger();
InBlock.gif            
if(pi.Ping(this.sPingAddress)<1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                evLog.WriteEntry(sPingAddress 
+" does not respond.");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public  class Pinger
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int  Ping (string addr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Socket sck
=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Net.IPHostEntry ipInfo
=System.Net.Dns.Resolve(addr);
InBlock.gif                IPEndPoint ipe
=new IPEndPoint(ipInfo.AddressList[0],8);
InBlock.gif                sck.Connect(ipe);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return -1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return 1;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/downmoon/archive/2008/08/29/1279154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值