Ejemplo n.º 1
0
        private async Task NotifyEmailAsync(AlarmOption option)
        {
            if (option.Emails == null || option.Emails.Count() == 0)
            {
                return;
            }

            if (Options.Mail == null || Options.Mail.Account.IsEmpty() || Options.Mail.Password.IsEmpty())
            {
                Logger.LogInformation("Mailbox cannot be empty");
                return;
            }

            if (option.Emails?.Any() == true)
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress("HttpReports", Options.Mail.Account));

                foreach (var to in option.Emails)
                {
                    message.To.Add(new MailboxAddress(to));
                }

                message.Subject = $"HttpReports - {Localize.Warning_Title}";

                message.Body = new TextPart(option.IsHtml ? TextFormat.Html : TextFormat.Plain)
                {
                    Text = option.Content
                };
                await SendMessageAsync(message);
            }
        }
Ejemplo n.º 2
0
        private async Task NotifyWebHookAsync(AlarmOption option)
        {
            try
            {
                if (option.WebHook.IsEmpty())
                {
                    return;
                }

                using (var httpClient = new HttpClient())
                {
                    string Title = $"HttpReports - {Localize.Warning_Title}";

                    HttpContent content = new StringContent(JsonConvert.SerializeObject(new { Title, option.Content }));
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                    var httpResponseMessage = await httpClient.PostAsync(option.WebHook, content);

                    if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Logger.LogInformation("WebHook Push Success");
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.LogInformation("WebHook Push Error:" + ex.Message, ex);
            }
        }
Ejemplo n.º 3
0
        public async Task AlarmAsync(AlarmOption option)
        {
            if (string.IsNullOrWhiteSpace(option.Content))
            {
                return;
            }

            if (MailOptions.Account.IsEmpty() || MailOptions.Password.IsEmpty())
            {
                Logger.LogInformation("预警邮件配置不能为空");
                return;
            }

            if (option.Emails?.Any() == true)
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress("HttpReports", MailOptions.Account));

                foreach (var to in option.Emails)
                {
                    message.To.Add(new MailboxAddress(to));
                }

                message.Subject = "HttpReports - 预警触发通知";

                message.Body = new TextPart(option.IsHtml ? TextFormat.Html : TextFormat.Plain)
                {
                    Text = option.Content
                };
                await SendMessageAsync(message).ConfigureAwait(false);
            }
        }
Ejemplo n.º 4
0
        private async Task NotifyAlarmAsync(AlarmOption option)
        {
            if (option.Alarm == null)
            {
                return;
            }

            await _storage.AddMonitorAlarm(option.Alarm);
        }
Ejemplo n.º 5
0
        public async Task AlarmAsync(AlarmOption option)
        {
            if (string.IsNullOrWhiteSpace(option.Content))
            {
                return;
            }

            await NotifyEmailAsync(option);

            await NotifyWebHookAsync(option);
        }