/// <summary> /// 发送邮件 /// </summary> /// <param name="TargetName">服务/进程名称</param> /// <param name="SystemName">服务/进程</param> /// <param name="msg">提示消息</param> public static async Task SendMail(string msg, string TargetName, string SystemName) { if (DogConfig.Instance.IsSend) { var MailContent = new MailContent(TargetName, msg, SystemName); foreach (var item in DogConfig.GetConfigMails()) { var hashValue = (MailContent.Target + item).GetHashCode(); var RecordModel = SendRecord.Where(it => it.hashValue == hashValue).FirstOrDefault(); if (RecordModel == null) { SendRecord.Add(new SendRecord() { TargetName = MailContent.Target, address = item, LastSendTime = DateTime.Now, hashValue = hashValue }); await Mail.SendAsync(item, "看门狗异常提醒", MailContent.content); } else if (RecordModel.LastSendTime.AddMinutes(Mail.config.MaxInterval) < DateTime.Now) { RecordModel.LastSendTime = DateTime.Now; await Mail.SendAsync(item, "看门狗异常提醒", MailContent.content); } } } }
/// <summary> /// 开始监控 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btStart_Click(object sender, EventArgs e) { this.btStart.Enabled = false; this.btStop.Enabled = true; if (this.cancellSource.IsCancellationRequested) { this.cancellSource = new CancellationTokenSource(); } Task.Run(() => { foreach (var item in DogConfig.GetConfigServiceNames()) { var control = new ServiceMonitor(item); control.StartAsync(this.cancellSource.Token); } }); Task.Run(() => { foreach (var item in DogConfig.GetConfigProcessPaths()) { var control = new ProcessMonitor(item); control.StartAsync(this.cancellSource.Token); } }); }
/// <summary> /// 服务启动任务 /// </summary> /// <param name="hostControl"></param> /// <returns></returns> public bool Start(HostControl hostControl) { Debugger.WriteLine("看门狗启动成功,开始监控!"); //服务类监控 foreach (var item in DogConfig.GetConfigServiceNames()) { var monitor = new ServiceMonitor(item); monitor.StartAsync(this.cancellSource.Token); } //程序类监控 foreach (var item in DogConfig.GetConfigProcessPaths()) { var monitor = new ProcessMonitor(item); monitor.StartAsync(this.cancellSource.Token); } return(true); }