c# - 获取 Azure 服务总线中的消息统计信息

标签 c# azure azureservicebus azure-servicebus-topics azure-servicebus-subscriptions

我正在编写一个实用程序来监视我们的 Azure 服务总线主题和订阅。

我可以获得主题详细信息,例如名称、排队消息计数和死信消息计数,但我想获取已处理的消息数。

这是我正在使用的代码:

var sub = namespaceManager.GetSubscription(topicPath, subscriptionName);

var name = sub.Name;
var pending= sub.MessageCountDetails.ActiveMessageCount;
var deadletter = sub.MessageCountDetails.DeadLetterMessageCount

似乎 GetSubscription 不包含任何用于获取已处理消息数的属性。

以前有人尝试过这样做吗?

最佳答案

要从 Azure Servicebus 实体获取消息统计信息,我使用 Visual Studio App Insights 。这是一个监控应用程序的工具。基本上,您的应用程序将事件发送到 App Insights 并从 Azure 门户,您可以 create dashboards为您提供有关应用程序的实时信息。

为了监视 Azure Servicebus 实体,我从我的应用程序发送自定义事件:

  • 您可以查看pricing ,有一个免费计划,允许您每月发送最多 500 万个自定义事件。如果您需要发送超过 500 万个事件,则可以在将事件发送到 App Insights 之前为每个 Servicebus 实体创建一个 App Insights 或聚合计数。
  • 您可以访问 7 天的原始数据和 90 天的汇总数据。

  • 如果您使用 Power BI,则可以配置 continuous export您的数据(不要认为它在免费计划中可用)。

  • 其他很酷的事情,您可以发送异常和 create alert from App Insigths只要 App Insigths 收到异常情况,就会向您发送电子邮件。

如果您处理来自网络作业/辅助角色/控制台应用程序/Windows 服务的服务总线消息,本文可能是一个很好的起点:

因此,从 Azure 门户创建 App Insights 后,您将获得一个InstrumentationKey

您可以安装ApplicationInsights来自 nuget。

要将事件发送到 App Insights,您需要实例化 TelemetryClient。 Microsoft 建议每个应用程序仅拥有一个遥测客户端实例,并在应用程序停止或重新启动时刷新 TelemetryClient:

var telemetryClient = new TelemetryClient()
    { InstrumentationKey = "MyInstrumentationKey" };

这是一个非常基本的示例,但您会明白要点:

// Get the message
BrokeredMessage message = ...

try
{
    // Process you message
    ...

    // Delete the message from the queue when it is ok.
    message.Complete();

    // Create and send an event to app insights
    var eventTelemetry = new EventTelemetry { Name = "MyQueueName" };
    eventTelemetry.Metrics["MessageCount"] = 1;
    telemetryClient.TrackEvent(eventTelemetry);
}
catch (Exception ex)
{
    // Send back the message to the queue ??? depends if you'd like to re-process it
    message.Abandon();

    // Send the exception to app insights
    telemetryClient.TrackException(ex);
}

使用此代码,您将在 App Insights 中拥有一个名为 MyQueueName 的新事件。您可以创建仪表板并过滤此事件并显示 消息计数指标。我使用指标是因为在更复杂的场景中,您可以每 x 分钟发送一个事件,并将 MessageCount 设置为在此时间间隔内处理的消息数。

在这里,我使用的是应用程序洞察,但我很确定您可以使用其他工具执行相同的操作,例如:

希望这对您有帮助!

关于c# - 获取 Azure 服务总线中的消息统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462261/

相关文章:

Azure DevOps : Selecting which variable group to use at the stage level

Azure 服务总线空闲时自动删除

azure - Windows Azure ServiceBus 中继澄清

c# - 从另一个 View 模型调用函数时 RaisePropertyChanged 不会触发

azure - 通过 TLS 从 Azure Function 使用 MQTT 触发器时出现问题

c# - 在 WebApi ApiController 上启用 OData v4 查询

Azure 语音服务 CLI,使用 SSML 错误代码 : 1007

java - ColdFusion 将枚举传递给 Java 方法

c# - 如何从 dataReader 列中读取 Xml 值

c# - 使用 XmlReader 加载外部 dtd