Not getting information level logs in my webapi

Viewed 500

I am using app insights for logging.

So i noticed that in traces i am only getting logs that are related to warning,error and critical but i need the info as well.

Below is my code

in program.cs

logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>(typeof(Program).FullName, LogLevel.Trace);

in appsettings.json i have the below rules

"NLog": {
    "autoReload": true,
    "throwConfigExceptions": true,
    "internalLogLevel": "warn",
    "internalLogFile": "${currentdir}/Logs/internal-nlog.txt",
    "extensions": [
      {
        "assembly": "NLog.Extensions.Logging"
      },
      {
        "assembly": "NLog.Web.AspNetCore"
      },
      {
        "assembly": "Microsoft.ApplicationInsights.NLogTarget"
      }
    ],
    "default-wrapper": {
      "type": "AsyncWrapper",
      "overflowAction": "Block"
    },
    "targets": {
      "blackHole": {
        "type": "Null",
        "formatMessage": false
      },
      "aiTarget": {
        "type": "ApplicationInsightsTarget"
      },
      "asyncWrapperConsoleTarget": {
        "type": "AsyncWrapper",
        "timeToSleepBetweenBatches": 0,
        "queueLimit": 1000000,
        "batchSize": 1000,
        "target": {
          "name": "ConsoleTarget",
          "type": "ColoredConsole",
          "layout": "CorrelationId= ${aspnet-item:variable=CorrelationId} ${aspnet-user-identity} ${date:universalTime=true}|${level:uppercase=true}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${message} ${exception:format=tostring}|${logger}|${all-event-properties}",
          "rowHighlightingRules": [
            {
              "condition": "level == LogLevel.Debug",
              "foregroundColor": "DarkGray"
            },
            {
              "condition": "level == LogLevel.Info",
              "foregroundColor": "Gray"
            },
            {
              "condition": "level == LogLevel.Warn",
              "foregroundColor": "Yellow"
            },
            {
              "condition": "level == LogLevel.Error",
              "foregroundColor": "Red"
            },
            {
              "condition": "level == LogLevel.Fatal",
              "foregroundColor": "Red",
              "backgroundColor": "White"
            }
          ]

        }
      },
 "rules": {
      "0": {
        "logger": "Microsoft.*",
        "minLevel": "debug",
        "writeTo": "blackHole",
        "final": true
      },
      "1": {
        "logger": "System.Net.Http.HttpClient.*",
        "minLevel": "info",
        "writeTo": "blackHole",
        "final": true
      },
      "2": {
        "logger": "*",
        "minLevel": "debug",
        "writeTo": "asyncWrapperDatabase"
      },
      "3": {
        "logger": "*",
        "minLevel": "trace",
        "writeTo": "aiTarget"
      }
    }

i have written the below code in my cs file

_logger.LogInformation("Testing info log");
            _logger.LogDebug("Testing debug log");
            _logger.LogError("Testing error log");
            _logger.LogTrace("Testing trace log");
            _logger.LogCritical("Testing critical log");
            _logger.LogWarning("Testing warning log");

From the above i only get logs for error, critical and warning. What is wrong that i am doing and why am i not getting information level logs?

1 Answers

There are 2 problems here.

  • Regarding app insights issue, you need to add app insights related config section in your appsettings.json (I see only NLog part in your shared config). Even though you are allowing at nlog level, but app insights adapter is filtering here. Default is Warning or above. As per official doc, something like below would fix your problem.
{
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  }
}

NOTE: ApplicationInsights/LogLevel should be nested inside the Logging as above if you notice carefuly.

  • Regarding the console logging issue, your rule has wrong target name. asyncWrapperDatabase should be asyncWrapperConsoleTarget.
Related