none
App Insight LogLevel in appsettings in ASP.NET Core 3.1 app is being ignored RRS feed

  • Question

  • We use ILogger in an ASP.NET Core 3.1 website, using the Microsoft.ApplicationInsights.AspNetCore package, version 2.14. We are trying to enable logging of informational messages into App Insight, ex. _logger.LogInformation("info here')

    In our startup in ConfigureServices, we enable App Insights:

    services.AddApplicationInsightsTelemetry();

    We have the following in our appsettings.json file:

    {
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },
        "ApplicationInsights": {
            "InstrumentationKey": "12345678-1234-5678-1234-1234567890ab",
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },

    It picks up the app insight key correctly (we do get the normal metrics, log entries such as exceptions), however none of our calls to logger.LogInformation("Info here") are being send/recorded in the App Insight dashboard.

    I found this question which is similar:

    https://stackoverflow.com/questions/51657398/ilogger-not-respecting-log-level-for-application-insights

    But the answer still doesn't really address how to be able to change the log level from the appsettings.json file vs. hard-coding the log level into the code.

    It seems like, from the docs, this should "just work", but it doesn't appear to.

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level

    Where are we going wrong?


    Monday, May 4, 2020 8:08 PM

All replies

  • Turns out the appsettings entry was wrong. It needs to look like this (ApplicationInsights nested within the Logging section):

    {
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            },
            "ApplicationInsights": {
                "LogLevel": {
                    "Default": "Information",
                    "Microsoft": "Warning",
                    "Microsoft.Hosting.Lifetime": "Information"
                }
            }
        },
        "ApplicationInsights": {
            "InstrumentationKey": "12345678-1234-5678-1234-1234567890ab"    
        },

    Wednesday, May 6, 2020 6:35 PM