我正在编写一个实用程序来监视我们的 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/