c# - 为什么 ILogger 不记录到 Azure 应用程序见解?

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

我正在直接从这里测试控制台应用程序的代码:https://learn.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.

            }
        }

最佳答案

代码是正确的,但您遇到了 ApplicationInsights 和控制台应用程序的已知问题 - 在 ApplicationInsights 可以将数据发送到后端之前应用程序已停止运行。 (数据不是立即发送,而是分批发送,间隔发送。)

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

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

但在 ILogger 情况下,您无法控制 TelemetryClient 实例。因此,最好的选择是控制 channel ,并在 channel 上调用flush,然后进行短暂的 sleep 。修改后的代码如下。

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

关于c# - 为什么 ILogger 不记录到 Azure 应用程序见解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289534/

相关文章:

.net - Windows Azure 获取 InstanceInputEndpoint 的外部 DNS

c# - 系统找不到用 NUnit3 控制台和 testparam 指定的文件

c# - 为什么这个 C# 函数的行为就像我在使用指针一样?

.NET Web API 2 和 IIS PUT/DELETE 不工作

c# - 如何从 linq 查询结果中忽略/删除非数字值 C#

azure - 如何在azure powershell中获取1天时间粒度的指标数据?

c# - Mock.Of<foo> 为带参数的方法调用设置返回值

c# - 谁负责缓存/内存功能结果?

.net - null == null 吗?

Azure BLOB 存储 - 许多事务