为什么ILogger不记录到Azure应用见解?

时间:2019-03-21 21:29:56

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

我正在此处直接针对控制台应用测试代码:https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#

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

 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.

            }
        }

1 个答案:

答案 0 :(得分:2)

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

增加约30秒的睡眠时间可以帮助您解决问题。 Thread.Sleep(31000);

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

但是在ILogger情况下,您无法控制TelemetryClient实例。因此,最好的选择是控制通道,并先在通道上刷新后再小睡。修改后的代码如下。

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);            
        }
    }