【问题标题】:Application Insights: Console Application HttpClient correlation not workingApplication Insights:控制台应用程序 HttpClient 关联不起作用
【发布时间】:2021-12-22 22:41:35
【问题描述】:

我有一个控制台应用程序,我计划使用 Application Insights 来启动遥测。此控制台应用程序在其中调用 Web API。

操作关联有效,但父层次结构无效。从本质上讲,Web API 调用的父级并不是来自控制台应用程序的初始调用。

下面是我的代码:

控制台应用

static async Task SendHttpOnly()
    {
        //Create TelemetryClient
        TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
        configuration.InstrumentationKey = "<id>";
        var telemetryClient = new TelemetryClient(configuration);

        RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "ConsoleTest" };

        var operation = telemetryClient.StartOperation(requestTelemetry);

        try
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:37970/");
                var responseTask = await client.PostAsJsonAsync<MessageDto>("MessageReceiver", new MessageDto() { Body = "Test" });
            }

        }
        catch (Exception e)
        {
            operation.Telemetry.Success = false;
            telemetryClient.TrackException(e);
            throw;
        }
        finally
        {
            telemetryClient.StopOperation(operation);
            telemetryClient.Flush();
            Task.Delay(5000).Wait();
        }

    }

网络 API

    [HttpPost]
    public string Post([FromBody] MessageDto dto)
    {
        _telemetryClient.TrackTrace($"Service Bus Message Processed: Message: {dto.Body}");
        return $"Processed { dto.Body }";
    }

奇怪的是,如果我对 Web API 进行 Web API 调用,它会正确记录它。即使使用相同的代码;第二个 Web API 调用父级是第一个 Web API 调用。

【问题讨论】:

  • 您能否检查 TraceParent 标头中通过线路发送的内容(以及它是否存在或状态是否通过不同的标头传递)?
  • 我意识到我使用了错误的 NuGet 包。我使用的是 Microsoft.ApplicationInsights.AspNetCore,而我应该将 Microsoft.ApplicationInsights.WorkerService 用于控制台应用程序。详情在这里:docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service
  • 我希望常规 SDK 也能在这里工作。

标签: asp.net-core azure-application-insights


【解决方案1】:

谢谢Water。很高兴您解决了您的问题并将其作为答案发布,以便对其他社区成员有所帮助。

由于使用了错误的 Nuget 包而不是使用下面的包,应用程序 HttpClient 关联不起作用

Microsoft.ApplicationInsights.AspNetCore

我们需要使用下面的包

Microsoft.ApplicationInsights.WorkerService

以下是在应用洞察中使用 SDK 的示例代码。

<ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.13.1" />
    </ItemGroup>

更多信息请查看Application Insight worker service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多