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

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


<!-- {cps..0}-->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.EventLogevLog;
private System.Timers.TimerTimerPing;


/**/ /// <summary>
/// 必需的设计器变量。
/// </summary>

private System.ComponentModel.Containercomponents = 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=NewSystem.ServiceProcess.ServiceBase[]{newService1(),newMySecondUserService()};
//
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( " pingServiceisStarting…………………………… " );
TimerPing.Interval
= this .iPingInterval;
this .TimerPing.Enabled = true ;
}

/**/ /// <summary>
/// 停止此服务。
/// </summary>

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

/**/ /// <summary>
/// 暂停
/// </summary>

protected override void OnPause()
{
this .evLog.WriteEntry( " pingServiceisPausing…………………………… " );
}

/**/ /// <summary>
/// 继续
/// </summary>

protected override void OnContinue()
{
this .evLog.WriteEntry( " pingServiceisContinuing…………………………… " );
}


private void TimerPing_Elapsed( object sender,System.Timers.ElapsedEventArgse)
{
Pingerpi
= new Pinger();
if (pi.Ping( this .sPingAddress) < 1 )
{
evLog.WriteEntry(sPingAddress
+ " doesnotrespond. " );
}

}

}


public class Pinger
{
public int Ping( string addr)
{
Socketsck
= new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
try
{
System.Net.IPHostEntryipInfo
= System.Net.Dns.Resolve(addr);
IPEndPointipe
= new IPEndPoint(ipInfo.AddressList[ 0 ], 8 );
sck.Connect(ipe);

}

catch
{
return - 1 ;
}

return 1 ;
}

}

}

你可能感兴趣的:(event)