【问题标题】:Why is ILogger not logging to Azure app insights?为什么 ILogger 不记录到 Azure 应用洞察?
【发布时间】:2019-03-21 21:29:56
【问题描述】:

我正在这里直接测试控制台应用程序的代码:https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#

我基本上复制了代码并将其指向一个新的 azure app insight 实例。但是,没有任何日志显示在应用程序洞察中。我错过了什么吗?

 static void Main(string[] args)
        {
            // Create DI container.
            IServiceCollection services = new ServiceCollection();

            // Add the logging pipelines to use. We are using Application Insights only here.
            services.AddLogging(loggingBuilder =>
            {
                // Optional: Apply filters to configure LogLevel Trace or above is sent to ApplicationInsights for all
                // categories.
                loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
                loggingBuilder.AddApplicationInsights(******);
            });

            // Build ServiceProvider.
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();


            logger.LogCritical("critical message working");
            // Begin a new scope. This is optional. Epecially in case of AspNetCore request info is already
            // present in scope.
            using (logger.BeginScope(new Dictionary<string, object> { { "Method", nameof(Main) } }))
            {
                logger.LogWarning("Logger is working - warning"); // this will be captured by Application Insights.

            }
        }

【问题讨论】:

  • @huysentruitw 不相关。这个问题是关于 Dotnet-Core Logging Extensions for AI 的。您链接到的是关于 AI 应用程序检测的。

标签: c# .net azure .net-core azure-application-insights


【解决方案1】:

代码是正确的,但您遇到了 ApplicationInsights 和控制台应用程序的一个已知问题 - 在 ApplicationInsights 可以将数据发送到后端之前,该应用程序已经死机。 (数据不会立即发送,而是批量发送,间隔发送。)

增加约 30 秒的睡眠时间应该会对您的情况有所帮助。 线程.睡眠(31000);

在常规控制台应用程序中,文档建议进行显式刷新。 https://docs.microsoft.com/en-us/azure/azure-monitor/app/console#full-example

但在 ILogger 案例中,您无法控制 TelemetryClient 实例。所以你最好的选择是控制通道,并在通道上调用flush,然后是一个小睡眠。修改后的代码如下。

class Program
    {
        static void Main(string[] args)
        {
            // Create DI container.
            IServiceCollection services = new ServiceCollection();

            var channel = new InMemoryChannel();

            services.Configure<TelemetryConfiguration>(
              (config) =>
                {
                    config.TelemetryChannel = channel;                    
                }
           );

            // Add the logging pipelines to use. We are using Application Insights only here.
            services.AddLogging(loggingBuilder =>
            {
                // Optional: Apply filters to configure LogLevel Trace or above is sent to ApplicationInsights for all
                // categories.
                loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
                loggingBuilder.AddApplicationInsights("***");
            });

            // Build ServiceProvider.
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();


            logger.LogCritical("critical message working");
            // Begin a new scope. This is optional. Epecially in case of AspNetCore request info is already
            // present in scope.
            using (logger.BeginScope(new Dictionary<string, object> { { "Method", nameof(Main) } }))
            {
                logger.LogWarning("Logger is working - warning"); // this will be captured by Application Insights.

            }

            channel.Flush();
            Thread.Sleep(1000);            
        }
    }

【讨论】:

  • 谢谢。当我为 logger.log 执行一个大的 for 循环时,我怀疑是这种情况……然后日志出现了。人们会认为结束记录器范围会刷新通道。
  • 默认情况下,通道每 30 秒刷新一次或缓冲 500 个项目。用每个范围刷新可能不是正确的方法,因为只发送少量项目可能效率低下。
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 2021-09-23
  • 2020-05-22
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多