Maroun
Maroun

Reputation: 95948

Enable DEBUG level logs of the Microsoft.AspNetCore namespace

I'm using log4net in my ASP.net Core application, and have the following configurations:

// Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((logging) => {
            logging.ClearProviders();
            logging.AddLog4Net();
        }).UseStartup<Startup>();

My config:

<log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-5date %-5level [%thread] %type.%method:%line - %message%newline" />
        </layout>
    </appender>
    
    <!-- log everything coming from AspNetCore -->
    <logger name="Microsoft.AspNetCore" additivity="false">
        <level value="Debug" />
        <appender-ref ref="Console" />
    </logger>
    
    <root>
        <level value="Info" />
        <appender-ref ref="Console" />
    </root>
</log4net>

What I want to achieve:

What am I missing? If I'm using log4net, do I still need the "Logging" section in the appsettings.json file? If I add the following, I will be able to see the logs, but why is it needed? Shouldn't this be controlled from the log4net.conf file?

"Logging": {
  "LogLevel": {
     "Microsoft": "Debug"
  }
}

Upvotes: 3

Views: 7344

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4778

The logging levels messages below the Information level for a development build you must make allowances for it in the appsettings.Development.json as specified in the documentation and illustrated below:

{
     "Logging": {
         "LogLevel": {
             "Default": "Debug",
             "...":"..."
          }
     }
}

See here

By default appsettings.json file will be generated in Asp.net core applications.

Logging configuration is commonly provided by the Logging section of appsettings.{Environment}.json files. appsettings.Development.json file is generated by the ASP.NET Core web app templates:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Debug",
            "Microsoft.Hosting.Lifetime": "Information"
          }
     }
}

The logging categories are

If the "default" log level is not set, the default log level value is Information.

The "Microsoft" category logs at log level Debug and higher.

The "Microsoft.Hosting.Lifetime" category logs at log level "Information" and higher.

The log level follows:

Trace = 0, Debug = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, and None = 6.

Refer here for more info

Check here for disable the IIS logs

Upvotes: 2

Related Questions