簡體   English   中英

Application Insights 不存儲 ILogger<> - 消息

[英]Application Insights not storing ILogger<> - messages

我有一個使用 Application Insights 的 .net 5 web 應用程序。 我嘗試使用ILogger<>登錄到 AI 跟蹤。 但是:在分析“痕跡”時 - Azure 上的 AI 中的內容不會顯示日志。

啟動部分:

services.AddLogging(loggingbuilder =>
  {
    loggingbuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
    loggingbuilder.AddApplicationInsights();
  });
services.AddApplicationInsightsTelemetry();          

應該進行日志記錄的 class 的構造函數通過依賴注入注入 ILogger 和 AppInsights:

public ImportService(ILogger<ImportService> log, TelemetryClient telemetryClient)
  {
    _log = log;
    _telemetryClient = telemetryClient;
  }

在該方法中,我有以下兩次日志記錄嘗試:

public async Task<Customer> UpdateCustomerByEmail(string email)
{
  _telemetryClient.TrackTrace("From Telemetry");
  _log.LogWarning("From Log");
  [...]
}

雖然第一個(“來自遙測”)最終在 AI 跟蹤中正確結束,但第二個(“來自日志”)從未出現在那里。

檢測密鑰存儲在 appsettings 中(顯然是正確的,因為 telemetryClient-Track 正在工作)

該文檔可能與您相關嗎? 將以下代碼添加到 program.cs 對我有用:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddApplicationInsights("<instrumentationKeyHere>");
                logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

還必須安裝 nuget package Microsoft.Extensions.Logging.ApplicationInsights。

在您的情況下,只需將 Instrumentation Key 作為參數提供給 AddApplicationInsights function 就足夠了。

如文檔中所述,“僅當您使用獨立的日志記錄提供程序時才需要此代碼。對於常規 Application Insights 監視,檢測密鑰從配置路徑 ApplicationInsights: Instrumentationkey 自動加載。”

這可能解釋了為什么定期監控有效,但不適用於日志記錄。

暫無
暫無

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

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