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
  • 相关阅读:
    【Git】Git 学习笔记(一)
    【工程 Shell】Shell 学习(一)
    Vue 使用 Antd 简单实现左侧菜单栏和面包屑功能
    GoF的23种设计模式的功能
    ASP 对数据库的操作
    注册表修改USB状态(开与关)
    EXE文件关联修复
    CentOS8安装Docker
    GoogleEarth无法连接服务器解决方法
    【转】Qt 实现的拷贝 文件/文件夹 的函数
  • 原文地址:https://www.cnblogs.com/downmoon/p/1279154.html
Copyright © 2011-2022 走看看