我们可以为服务总线触发器Azure v4函数c#设置ConversationID吗?在 ITelemetryInitializer 中

问题描述 投票:0回答:1

我在 c# 中使用 Azure Function V4 隔离模式。其服务总线触发功能。 在消息正文中,我已经传递了 ConversationID。

我已经添加了遥测。

services.AddSingleton<ITelemetryInitializer, AppSQLInsightsPropertiesTelemetryInitializer>();
  services.AddApplicationInsightsTelemetryWorkerService();
  services.ConfigureFunctionsApplicationInsights(); 

  public class AppSQLInsightsPropertiesTelemetryInitializer : ITelemetryInitializer
  {

      public AppSQLInsightsPropertiesTelemetryInitializer()
      {
              
      }
      public void Initialize(ITelemetry telemetry)
      {
          telemetry.Context.GlobalProperties["ConversationID"] = "how i can get servicebus message ConversationID ";
      }
  }

我们如何读取服务总线消息 ConversationID 属性,并在此处添加我们将在此服务总线触发功能中使用的所有日志。 任何人都有想法。

azure-functions azure-application-insights azureservicebus azure-servicebus-queues azure-functions-isolated
1个回答
0
投票

您可以读取正在发送的服务总线消息的 ConversationID 属性。

Enter image description here

下面的代码从 Azure 函数隔离 C# 函数中的服务总线消息访问

ConversationID
属性:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Azure.Messaging.ServiceBus;

public class Function1
{
    private readonly ILogger<Function1> _logger;

    public Function1(ILogger<Function1> logger)
    {
        _logger = logger;
    }
}

public class ServiceBusTriggerFunction
{
    private readonly TelemetryClient _telemetryClient;

    public ServiceBusTriggerFunction(TelemetryConfiguration telemetryConfiguration)
    {
        _telemetryClient = new TelemetryClient(telemetryConfiguration);
    }

    [Function(nameof(ServiceBusTriggerFunction))]
    public void Run(
        [ServiceBusTrigger("certificate122", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message,
        FunctionContext context)
    {
        var logger = context.GetLogger<ServiceBusTriggerFunction>();

        // Extract ConversationID from the message
        if (message.ApplicationProperties.TryGetValue("ConversationID", out var conversationId))
        {
            // Add ConversationID to telemetry properties
            _telemetryClient.Context.GlobalProperties["ConversationID"] = conversationId.ToString();
            // Log ConversationID
            logger.LogInformation("ConversationID1: {conversationId}", conversationId);
        }
        else
        {
            logger.LogInformation("ConversationID not found in message properties.");
        }

        
        logger.LogInformation("Custom Properties:");
        foreach (var property in message.ApplicationProperties)
        {
            logger.LogInformation("{Key}: {Value}", property.Key, property.Value);
        }

        
        logger.LogInformation("Message ID: {id}", message.MessageId);
        logger.LogInformation("Message Body: {body}", message.Body);
        logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);

        
    }
}

输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.