繁体   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