0

我在我的 StartUp.cs 中配置了这个:

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .ConfigureEmail(Configuration)
            .AddHealthChecksUI(setupSettings: setup =>
            {
                setup
                    .AddWebhookNotification("WebHookTest", "/WebhookNotificationError",
                        "{ message: \"Webhook report for [[LIVENESS]]: [[FAILURE]] - Description: [[DESCRIPTIONS]]\"}",
                        "{ message: \"[[LIVENESS]] is back to life\"}",
                        customMessageFunc: report =>
                        {
                            var failing = report.Entries.Where(e => e.Value.Status == UIHealthStatus.Unhealthy);
                            return $"{failing.Count()} healthchecks are failing";
                        },
                        customDescriptionFunc: report =>
                        {
                            var failing = report.Entries.Where(e => e.Value.Status == UIHealthStatus.Unhealthy);
                            return $"HealthChecks with names {string.Join("/", failing.Select(f => f.Key))} are failing";
                        });
            })
            .AddControllers();
    }

    public void Configure(IApplicationBuilder app)
    {
        var pathBase = Configuration["PATH_BASE"];
        if (!string.IsNullOrEmpty(pathBase))
        {
            app.UsePathBase(pathBase);
        }
    
        app.ConfigureExceptionHandler();
        app
            .UseRouting()
            .UseEndpoints(endpoints =>
            {
                endpoints.MapHealthChecksUI(options =>
                {
                    options.ResourcesPath = string.IsNullOrEmpty(pathBase) ? "/ui/resources" : $"{pathBase}/ui/resources";
                    options.UIPath = "/hc-ui";
                    options.ApiPath = "/api-ui";
                });
                endpoints.MapDefaultControllerRoute();
            });
    }

在控制器中:

    [HttpPost]
    [Consumes(MediaTypeNames.Application.Json)]
    public async Task<IActionResult> WebhookNotificationError([FromBody] string id)
    {
        MimeMessage mimeMessage = new MimeMessage { Priority = MessagePriority.Urgent };
        mimeMessage.To.Add(new MailboxAddress(_configuration.GetValue<string>("ConfiguracionCorreoBase:ToEmail")));
        mimeMessage.Subject = "WebHook Error";
        BodyBuilder builder = new BodyBuilder { HtmlBody = id };
        mimeMessage.Body = builder.ToMessageBody();
        await _appEmailService.SendAsync(mimeMessage);
        return Ok();
    }

看门狗应用程序在 appSettings.json 中配置为侦听不同的 API。到目前为止一切正常,但是,如果我强制出错,我希望收到一封通知电子邮件。这个想法是,当任何健康状况发生错误时,您会发送一封电子邮件。

环境:

  • .NET 核心版本:3.1
  • 健康检查版本:AspNetCore.HealthChecks.UI 3.1.0
  • 操作系统:Windows 10
4

2 回答 2

1

看起来你的路线有问题。您是否使用 Postman 验证了该方法?还要检查您的 webHook 请求正文是否为文本,尝试更改您的模板有效负载:

{ "message" : "[[LIVENESS]] 的 Webhook 报告:[[FAILURE]] - 描述:[[DESCRIPTIONS]]"}",

并在控制器中将字符串更改为对象。并检查您在 DEBUG 中收到的内容。

于 2020-07-16T21:51:47.203 回答
0

尝试使用Api/WebhookNotificationErrorinst. 如果您的/WebhookNotificationError控制器是ApiController. 控制器名称似乎丢失

于 2020-10-01T10:26:38.063 回答