读书人

一个系统日志EventLog的示例

发布时间: 2014-03-14 00:29:37 作者: rapoo


[c-sharp] view plaincopyprint?
  1. using System;

  2. using System.Collections;

  3. using System.ComponentModel;

  4. using System.Data;

  5. using System.Diagnostics;

  6. using System.ServiceProcess;

  7. using System.Net;

  8. using System.Net.Sockets;

  9. namespace wsPing

  10. {

  11. public class pingService : System.ServiceProcess.ServiceBase

  12. {

  13. public System.Diagnostics.EventLog evLog;

  14. private System.Timers.Timer TimerPing;

  15. /**//// <summary>

  16. /// 必需的设计器变量。

  17. /// </summary>

  18. private System.ComponentModel.Container components = null;

  19. //ping 一个页面地址*********************************

  20. int iPingInterval =180000;//3分钟

  21. string sPingAddress="www.reaDer8.cn";

  22. //**************************************************

  23. public pingService()

  24. {

  25. // 该调用是 Windows.Forms 组件设计器所必需的。

  26. InitializeComponent();

  27. // TODO: 在 InitComponent 调用后添加任何初始化

  28. //如果不存在日志************************************

  29. if(!System.Diagnostics.EventLog.SourceExists("logService"))

  30. {

  31. EventLog.CreateEventSource("logService","logServiceLog");

  32. }

  33. this.evLog.Source="logService";

  34. //如果要重新命名,必须重新启动计算机

  35. //this.evLog.Log="logServiceLog";

  36. //**************************************************

  37. }

  38. // 进程的主入口点

  39. static void Main()

  40. {

  41. System.ServiceProcess.ServiceBase[] ServicesToRun;

  42. // 同一进程中可以运行多个用户服务。若要将

  43. //另一个服务添加到此进程,请更改下行

  44. // 以创建另一个服务对象。例如,

  45. //

  46. // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};

  47. //

  48. ServicesToRun = new System.ServiceProcess.ServiceBase[] { new pingService() };

  49. System.ServiceProcess.ServiceBase.Run(ServicesToRun);

  50. }

  51. /**//// <summary>

  52. /// 设计器支持所需的方法 - 不要使用代码编辑器

  53. /// 修改此方法的内容。

  54. /// </summary>

  55. private void InitializeComponent()

  56. {

  57. this.evLog = new System.Diagnostics.EventLog();

  58. this.TimerPing = new System.Timers.Timer();

  59. ((System.ComponentModel.ISupportInitialize)(this.evLog)).BeginInit();

  60. ((System.ComponentModel.ISupportInitialize)(this.TimerPing)).BeginInit();

  61. //

  62. // TimerPing

  63. //

  64. this.TimerPing.Interval = 60000;

  65. this.TimerPing.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerPing_Elapsed);

  66. //

  67. // pingService

  68. //

  69. this.CanHandlePowerEvent = true;

  70. this.CanPauseAndContinue = true;

  71. this.CanShutdown = true;

  72. this.ServiceName = "pingService";

  73. ((System.ComponentModel.ISupportInitialize)(this.evLog)).EndInit();

  74. ((System.ComponentModel.ISupportInitialize)(this.TimerPing)).EndInit();

  75. }

  76. /**//// <summary>

  77. /// 清理所有正在使用的资源。

  78. /// </summary>

  79. protected override void Dispose( bool disposing )

  80. {

  81. if( disposing )

  82. {

  83. if (components != null)

  84. {

  85. components.Dispose();

  86. }

  87. }

  88. base.Dispose( disposing );

  89. }

  90. /**//// <summary>

  91. /// 设置具体的操作,以便服务可以执行它的工作。

  92. /// </summary>

  93. protected override void OnStart(string[] args)

  94. {

  95. // TODO: 在此处添加代码以启动服务。

  96. this.evLog.WriteEntry("pingService is Starting……………………………");

  97. TimerPing.Interval=this.iPingInterval;

  98. this.TimerPing.Enabled=true;

  99. }

  100. /**//// <summary>

  101. /// 停止此服务。

  102. /// </summary>

  103. protected override void OnStop()

  104. {

  105. // TODO: 在此处添加代码以执行停止服务所需的关闭操作。

  106. this.evLog.WriteEntry("pingService is Stopping……………………………");

  107. this.TimerPing.Enabled=false;

  108. }

  109. /**//// <summary>

  110. /// 暂停

  111. /// </summary>

  112. protected override void OnPause()

  113. {

  114. this.evLog.WriteEntry("pingService is Pausing……………………………");

  115. }

  116. /**//// <summary>

  117. ///继续

  118. /// </summary>

  119. protected override void OnContinue()

  120. {

  121. this.evLog.WriteEntry("pingService is Continuing……………………………");

  122. }

  123. private void TimerPing_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

  124. {

  125. Pinger pi=new Pinger();

  126. if(pi.Ping(this.sPingAddress)<1)

  127. {

  128. evLog.WriteEntry(sPingAddress +" does not respond.");

  129. }

  130. }

  131. }

  132. public class Pinger

  133. {

  134. public int Ping (string addr)

  135. {

  136. Socket sck=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);

  137. try

  138. {

  139. System.Net.IPHostEntry ipInfo=System.Net.Dns.Resolve(addr);

  140. IPEndPoint ipe=new IPEndPoint(ipInfo.AddressList[0],8);

  141. sck.Connect(ipe);

  142. }

  143. catch

  144. {

  145. return -1;

  146. }

  147. return 1;

  148. }

  149. }

  150. }


读书人网 >软件开发

热点推荐