为什么我看不到任何日志条目,但仍然可以在 Application Insights 中看到实时指标的变化?

发布于 2025-01-13 04:53:22 字数 3233 浏览 1 评论 0原文

我按照这个微软教程来实现应用程序我将用作网络作业的控制台应用程序的洞察日志记录。我的代码有几个问题。

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        public static void Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection();

            // Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
            // Hence instrumentation key and any changes to default logging level must be specified here.
            services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Trace));
            services.AddApplicationInsightsTelemetryWorkerService("xyz-xzy-xyz-xyz-xyz");

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

            // Obtain logger instance from DI.
            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();

            // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
            var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

            int i = 0;
            
            while (true) // This app runs indefinitely. replace with actual application termination logic.
            {
                
                // Replace with a name which makes sense for this operation.
                using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
                {
                    telemetryClient.TrackTrace("VS This is tracktrace from telemetry" + i);//
                    logger.LogWarning("VS Logger.logWarning" + i);//
                    logger.LogInformation("VS logger.LogInformation" + i);
                    logger.LogTrace("VS logger.LogTrace" + i);
                    logger.LogError("VS logger.LogError" + i);//
                    logger.LogDebug("VS logger.LogDebug" + i);
                    logger.LogCritical("VS logger.LogCritical" + i);//
                    telemetryClient.Flush();
                    Task.Delay(5000).Wait();
                }
                i = i + 1;
            }
        }
    }
}

首先,我收到实时指标日志,如下面的屏幕截图所示: 输入图片这里的描述

但是当我在日志中运行traces命令时,它没有显示任何内容,如下所示: 输入图片此处描述

其次,为什么我没有看到 logger.LogInformationlogger.LogTracelogger.LogDebug 的任何日志?我已将日志级别设置为跟踪(LogLevel.Trace)。

顺便说一句,我在最后使用 Task.Delay(5000).Wait(); 以确保所有日志都刷新到我的 Application Insight。这对于生产环境来说也是正确的方法吗?

I followed this microsoft tutorial to implement Application Insight logging for my console application which I will be using as webjob. I have a couple of issues with the code.

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        public static void Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection();

            // Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
            // Hence instrumentation key and any changes to default logging level must be specified here.
            services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Trace));
            services.AddApplicationInsightsTelemetryWorkerService("xyz-xzy-xyz-xyz-xyz");

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

            // Obtain logger instance from DI.
            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();

            // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
            var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

            int i = 0;
            
            while (true) // This app runs indefinitely. replace with actual application termination logic.
            {
                
                // Replace with a name which makes sense for this operation.
                using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
                {
                    telemetryClient.TrackTrace("VS This is tracktrace from telemetry" + i);//
                    logger.LogWarning("VS Logger.logWarning" + i);//
                    logger.LogInformation("VS logger.LogInformation" + i);
                    logger.LogTrace("VS logger.LogTrace" + i);
                    logger.LogError("VS logger.LogError" + i);//
                    logger.LogDebug("VS logger.LogDebug" + i);
                    logger.LogCritical("VS logger.LogCritical" + i);//
                    telemetryClient.Flush();
                    Task.Delay(5000).Wait();
                }
                i = i + 1;
            }
        }
    }
}

Firstly, I am receiving logs in live metrics as shown in the screenshot below:
enter image description here

But when I am running the traces command in logs it is not showing anything as shown below:
enter image description here

Secondly, Why I am not seeing any log of logger.LogInformation, logger.LogTrace and logger.LogDebug? I have set my log level to trace(LogLevel.Trace).

By the way, I am using Task.Delay(5000).Wait(); at the end to make sure all logs get flushed to my Application Insight. Is it the right approach for the Production environment as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2025-01-20 04:53:22
  • 首先检查以下几点
  1. 检查您选择的时间范围
  2. 重新部署您的应用程序。
  3. 如果它仍然不起作用,请检查下面提到的代码。
  • 在我的本地环境中进行测试后,请找到下面的代码,它将为您提供实时指标以及来自跟踪的数据
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.WorkerService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace appinsightsinwebjob
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stoptwatch = new Stopwatch();
            stoptwatch.Start();
            IServiceCollection services = new ServiceCollection();
            //var aiOptions = new ApplicationInsightsServiceOptions();
            // Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
            // Hence instrumentation key and any changes to default logging level must be specified here.
            services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Trace));
            services.AddApplicationInsightsTelemetryWorkerService("5164e66d-fe14-403e-bc2f-6247ebedeb38");
            //services.AddApplicationInsightsTelemetryWorkerService(aiOptions);
            // Build ServiceProvider.
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            // Obtain logger instance from DI.
            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();

            // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
            var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

            int i = 0, j = 5;
            for (i = 0; i < j; i++)
            {
                // Replace with a name which makes sense for this operation.
                using (telemetryClient.StartOperation<RequestTelemetry>(""))
                {
                    telemetryClient.TrackTrace("VS OK This is tracktrace from telemetry" + i);//
                    logger.LogWarning("VS OK Logger.logWarning" + i);//
                    logger.LogInformation("VS OK logger.LogInformation" + i);
                    logger.LogTrace("VS OK logger.LogTrace" + i);
                    logger.LogError("VS OK logger.LogError" + i);//
                    logger.LogDebug("VS OK logger.LogDebug" + i);
                    logger.LogCritical("VS OK logger.LogCritical" + i);//
                    telemetryClient.Flush();
                    Thread.Sleep(5000);
                }
            }

        }

这是跟踪输出

在此处输入图像描述

  • 是的,你可以 Task.Delay(5000).Wait( ); 生产环境中的命令甚至我的代码中使用的延迟命令。
  • Firstly check below points
  1. Check the time range you selected
  2. Redeploy your application.
  3. if it still not working then check the below code mentioned.
  • After testing in my local environment please find the below code which will give you live metrics as well as data from traces

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.WorkerService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace appinsightsinwebjob
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stoptwatch = new Stopwatch();
            stoptwatch.Start();
            IServiceCollection services = new ServiceCollection();
            //var aiOptions = new ApplicationInsightsServiceOptions();
            // Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
            // Hence instrumentation key and any changes to default logging level must be specified here.
            services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Trace));
            services.AddApplicationInsightsTelemetryWorkerService("5164e66d-fe14-403e-bc2f-6247ebedeb38");
            //services.AddApplicationInsightsTelemetryWorkerService(aiOptions);
            // Build ServiceProvider.
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            // Obtain logger instance from DI.
            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();

            // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
            var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

            int i = 0, j = 5;
            for (i = 0; i < j; i++)
            {
                // Replace with a name which makes sense for this operation.
                using (telemetryClient.StartOperation<RequestTelemetry>(""))
                {
                    telemetryClient.TrackTrace("VS OK This is tracktrace from telemetry" + i);//
                    logger.LogWarning("VS OK Logger.logWarning" + i);//
                    logger.LogInformation("VS OK logger.LogInformation" + i);
                    logger.LogTrace("VS OK logger.LogTrace" + i);
                    logger.LogError("VS OK logger.LogError" + i);//
                    logger.LogDebug("VS OK logger.LogDebug" + i);
                    logger.LogCritical("VS OK logger.LogCritical" + i);//
                    telemetryClient.Flush();
                    Thread.Sleep(5000);
                }
            }

        }

And here is the traces Output

enter image description here

  • Yes you can you Task.Delay(5000).Wait(); command in the production environment even delay command used in my code.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文