簡體   English   中英

Application Insights 不顯示來自 .NET6 輔助角色服務的數據

[英]Application Insights not displaying data from .NET6 worker service

我完全絕望了,這可能只是我遺漏的一個小細節,但我無法弄清楚。

描述

我正在關注Application Insights for Worker Service 應用程序(非 HTTP 應用程序) ,這里有一些詳細信息。
我的 package 參考資料:

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" /> 
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.0" />

我的秘密.json

"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=xxxx;IngestionEndpoint=https://somewhere-0.in.applicationinsights.azure.com/",
  }

代碼遵循提到的鏈接,但只是把它放在這里。 程序.cs

services.AddLogging();
services.AddHostedService<TimedHostedService>();
services.AddApplicationInsightsTelemetryWorkerService();
 

工人.cs

_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
using (_telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
   _logger.LogWarning("A sample warning message.");
   _logger.LogInformation("Calling bing.com");
   var res = await _httpClient.GetAsync("https://bing.com");
   _logger.LogInformation("Calling bing, status:" + res.StatusCode);
   _telemetryClient.TrackEvent("Bing call event completed");
   _telemetryClient.Flush();
}
await Task.Delay(5000, stoppingToken);

在 Visual Studio 中,我可以看到事件正在發生。

Visual Studio 應用程序洞察

問題

幾天后,我無法在 Azure 門戶中獲得任何應用程序。 我想要一個 web 應用程序和一些容器來報告相同的日志分析和 azure 應用程序洞察力。 然而,到目前為止還沒有運氣。
部署到 Azure 應用服務的 web 應用報告正常。
這些容器應該在本地數據中心(內部部署)上運行,但現在我在 Docker 桌面上運行它們。

問題。

我根本不知道如何弄清楚我錯過了什么,所以這只是一系列突然出現在我腦海中的問題。 Docker 是否有什么阻塞? 我必須在 Azure 門戶中進行一些調整嗎? 我需要在我的電腦上設置一些東西嗎? 某處是否有設置表明該應用程序不應傳輸到應用程序洞察力,因為它是一個開發環境? (我可以永遠使用 go)。
我很確定這有一個簡單的答案,但這些天谷歌不是我的朋友。
任何幫助將不勝感激。

我遵循了你分享的同一個MSDOC 我可以在 Application Insights 中獲取所有遙測數據。 我希望您遵循文檔中提到的所有內容。

請檢查以下內容以解決問題

  • 檢查您的Package References您使用的所有包是什么,確保將它們添加到您的解決方案中。
  • 確保App Insights Connection 設置值應該有效
  • Program.cs中添加WorkerService ,例如
services.AddLogging(<Your Logger Provider>);
# make sure to access of your Hosted Service
services.AddHostedService<TimedHostedService>();
services.AddApplicationInsightsTelemetryWorkerService();

請注意,此 SDK 中的ApplicationInsightsServiceOptions位於命名空間Microsoft.ApplicationInsights.WorkerService中,與ASP.NET Core SDK 中的Microsoft.ApplicationInsights.AspNetCore.Extensions相對。

  • 在 Application Insights 中處理自定義操作跟蹤
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
using (_telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
   #_logger will be appear on Console 
   
   _logger.LogWarning("A sample warning message.");
   _logger.LogInformation("Calling bing.com");
   var res = await _httpClient.GetAsync("https://bing.com");
   _logger.LogInformation("Calling bing, status:" + res.StatusCode);
  
   # _telemetryClient will be appear in Application Insights
   
   _telemetryClient.TrackEvent("Bing call event completed");
   _telemetryClient.Flush();
}
await Task.Delay(5000, stoppingToken);

請參考Application Insights 中自定義操作跟蹤的用法

services.AddApplicationInsightsTelemetryWorkerService(); 使用此方法時,連接字符串必須是應用程序的 IConfiguration 的一部分。 我不確定 secrets.json 是否自動加載到 IConfiguration 中。

您可以使用以下代碼段來確認這一點:

 var aiOptions = new ApplicationInsightsServiceOptions();
 aiOptions.ConnectionString = "put it here";
 services.AddApplicationInsightsTelemetryWorkerService(aiOptions);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM