I can't seem to get ILogger.LogDebug() to write using Log4Net and Microsoft.Extensions.Logging.Log4Net.AspNetCore in a .Net 7 console app. I am confident that I'm setting the 'level' element in the log4net.config file correctly to 'DEBUG'. I can change the level to 'WARN' and this omits information messages as expected. If changing to 'DEBUG' or 'ALL' no debug messages are written out to console or file.

The issue is similar as described here: log4net debug logs are not shown even if debug level is set to true but I am not overriding the settings in any other config file (confirmed through the output from Log4Net debug).

Extensions and Versions used

Log4Net : 2.0.15
Microsoft.Extensions.Logging: 7.0.0
Micorosft.Extensions.Logging.Console : 7.0.0
Microsoft.Extensions.Logging.Log4Net.AspNetCore : 6.1.0

Setup in program.cs

var builder = Host.CreateApplicationBuilder();
...
builder.Logging.ClearProviders();
var loggingOptions = new Log4NetProviderOptions()
{
    Watch = false,
    Log4NetConfigFileName = Path.Combine(AppContext.BaseDirectory, "log4net.config")
};
builder.Logging.AddLog4Net(loggingOptions);

Log4Net Config snippet

  <root>
    <level value = "DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>

Inside a class injected with the ILogger:

_logger.LogCritical("Critial message");
_logger.LogDebug("Debug message");
_logger.LogError("Error message");
_logger.LogInformation("Information message");
_logger.LogTrace("Trace message");
_logger.LogWarning("Warning message");

output

2023-07-17 08:04:22,325 [1] FATAL myapp.console.Verbs.TestVerb - Critical message
2023-07-17 08:04:22,381 [1] ERROR myapp.console.Verbs.TestVerb - Error message
2023-07-17 08:04:22,383 [1] INFO myapp.console.Verbs.TestVerb - Information message
2023-07-17 08:04:22,399 [1] WARN myapp.console.Verbs.TestVerb - Warning message

Log4Net debug snippet

log4net: log4net assembly [log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a].
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [Microsoft.Extensions.Logging.Log4Net.AspNetCore, Version=6.1.0.0, Culture=neutral, PublicKeyToken=5d1104efbd0e675d]
log4net: Assembly [Microsoft.Extensions.Logging.Log4Net.AspNetCore, Version=6.1.0.0, Culture=neutral, PublicKeyToken=5d1104efbd0e675d] Loaded From [C:\git\myapp\myapp.console\bin\Debug\net7.0\win-x64\Microsoft.Extensions.Logging.Log4Net.AspNetCore.dll]
log4net: Assembly [Microsoft.Extensions.Logging.Log4Net.AspNetCore, Version=6.1.0.0, Culture=neutral, PublicKeyToken=5d1104efbd0e675d] does not have a RepositoryAttribute specified.
log4net: Assembly [Microsoft.Extensions.Logging.Log4Net.AspNetCore, Version=6.1.0.0, Culture=neutral, PublicKeyToken=5d1104efbd0e675d] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
...
log4net: Logger [root] Level string is [DEBUG].
log4net: Logger [root] level set to [name="DEBUG",value=30000].
...
log4net: Opening file for writing [C:\ProgramData\myapp\log.txt] append [True]
log4net: Created Appender [file]
log4net: Adding appender named [file] to logger [root].

I can change the log level by changing to WARN for example:

  <root>
    <level value = "WARN" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>

With the 'information' level now omitted as expected.

2023-07-17 08:18:43,480 [1] FATAL myapp.console.Verbs.TestVerb - Critial message
2023-07-17 08:18:43,522 [1] ERROR myapp.console.Verbs.TestVerb - Error message
2023-07-17 08:18:43,525 [1] WARN myapp.console.Verbs.TestVerb - Warning message
1

There are 1 best solutions below

0
On

Turns out that by adding:

builder.Logging.SetMinimumLevel(LogLevel.Debug);

immediately above the builder.Logging.AddLog4Net(loggingOptions) line fixed the problem.

I got this clue from following the link to https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore/issues/70 from log4net debug logs are not shown even if debug level is set to true gave me some more configurations to try.