Application Insights LogInformation not working

Viewed 1579

I have started to use application insights to log messages from a console application. Only critical and errors are being logged. Information or trace are not being logged. Any ideas of why information is excluded?

 class Program
{
    static void Main(string[] args)
    {

        // Create the DI container.
        IServiceCollection services = new ServiceCollection();


        services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));

        services.AddApplicationInsightsTelemetryWorkerService("653ac7cb-7563-42fb-ba06-0bd098bcd67c");

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

        var factory = serviceProvider.GetRequiredService<ILoggerFactory>();

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

        var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

        using (telemetryClient.StartOperation<RequestTelemetry>("Firefly.Core.ClientRunner.Worker.exe"))
        {

            logger.LogCritical("Critical A");
            logger.LogDebug("Debug A");
            logger.LogError("Error A");
            logger.LogInformation("Information A", "item 1", "item 2", 4);
            logger.LogTrace("Trace A");
            telemetryClient.Flush();
            Task.Delay(5000).Wait();
        }


    }
}
2 Answers

Only Warning or above severity logs are captured by default. It looks like you are trying to collect logs of Information and above, but your code below is doing it only for category named "Category".

services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));

Replace it with actual category name, or use the following to get from all categories.

logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                        ("", LogLevel.Information)

You can also customize these settings using appsettings.json, just add the following but note that the settings must be nested within the Logging node.

{
    "Logging": {
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information",
            //you can also define custom behavior by namespace, like if you don't want the thousands of messages you'll get from HttpClient
            "System.Net.Http.HttpClient": "Warning"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Debug",
                "System": "Information",
                "Microsoft": "Information",
                //you can also define custom behavior by namespace, like if you don't want the thousands of messages you'll get from HttpClient
                "System.Net.Http.HttpClient": "Warning"
            }
        }
    }
}
Related