'Why I can't see any log entry but still can see the changes in live metrics in Application Insights?

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?



Solution 1:[1]

  • 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaiSakethGuduru-MT