Nlog: Logger.LogDebug and below does not output anything

Created on 11 Apr 2020  路  4Comments  路  Source: NLog/NLog

Hi,

I got this issue recently at some time after upgrading the NuGet-packages for NLog to 4.7.0 and NLog.Extensions.Logging to 1.6.2 and adding an event log to my logger factory for my .Net Framework 4.8 project.

Tl;dr of the issue is: Logger.LogDebug("message") and Logger.LogTrace("message") stopped writing anything to anywhere even if the log level matches. Switching the statement out for e.g. Logger.LogInformation("message") at the same place works just fine. It has definitely worked before with no changes apart from the upgrading and event-log-adding.

My NLog.config gets loaded correctly (had a look at the internal logging and it works fine) and looks like this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">
  <targets>
    <target name="logfile" xsi:type="File" fileName="test.log" />
    <target name="logconsole" xsi:type="Console" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="logconsole" />
    <logger name="*" minlevel="Warn" writeTo="logfile" />
  </rules>
</nlog>

I've played with the minlevel of both the console and the file output, only log messages above "Info" are logged to either. Setting the minlevel to other values like "Warn" will only log the matching the minimum level, so the setting "kind of works".

I set up my logging like this:

eventLogSettings.Filter = (_, logLevel) => logLevel >= LogLevel.Debug;
LoggerFactoryInstance = LoggerFactory.Create(builder =>
    {
        builder.AddProvider(new NLogLoggerProvider())
            .AddEventLog(eventLogSettings);
    });

and in other classes get my loggers like this

var Logger = LoggerFactoryInstance.CreateLogger("some category");

which I then try to write to with

Logger.LogDebug("some message");

Again: If I am switching this for e.g. Logger.LogInformation("message") anywhere it will work just fine.

Before stopping the application I also execute a

NLog.LogManager.Shutdown();

I've removed the event log line at the factory creation for debugging but it did not change anything with this issue.

My program is of the type "console application" and gets debugged by me as one but usually runs a windows service. Neither logs debug messages "correctly".

Please help me out with this, I will gladly provide any information you need.

question

Most helpful comment

Thank you, got it to work with your hint! Although I am not building an .Net Core application, but a .Net Framework service.

For completeness sake the missing piece:

// Set event log filtering to "information".
var eventLogSettings = new EventLogSettings()
{
    SourceName = "ITS.APE ArtifactDetector Service"
};
// Set event log filtering to "information".
eventLogSettings.Filter = (_, logLevel) => logLevel >= LogLevel.Debug;

// Add factory with NLog and Windows EventLog.
LoggerFactoryInstance = LoggerFactory.Create(builder =>
{
    builder.SetMinimumLevel(LogLevel.Trace);
    builder.AddProvider(new NLogLoggerProvider())
        .AddEventLog(eventLogSettings);
});

The crucial part is setting the log level with builder.SetMinimumLevel to the lowest level possible and doing the filtering within the providers themselves.

All 4 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

When integration NLog with Microsoft Extension Logging (MEL), then one have to be aware of the MEL-filters.

So make sure to configure appsettings.json and the environment specific filters: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3#5-configure-appsettingsjson

Since the default SetMinimumLevel() can be overridden: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3#4-update-programcs

Thank you, got it to work with your hint! Although I am not building an .Net Core application, but a .Net Framework service.

For completeness sake the missing piece:

// Set event log filtering to "information".
var eventLogSettings = new EventLogSettings()
{
    SourceName = "ITS.APE ArtifactDetector Service"
};
// Set event log filtering to "information".
eventLogSettings.Filter = (_, logLevel) => logLevel >= LogLevel.Debug;

// Add factory with NLog and Windows EventLog.
LoggerFactoryInstance = LoggerFactory.Create(builder =>
{
    builder.SetMinimumLevel(LogLevel.Trace);
    builder.AddProvider(new NLogLoggerProvider())
        .AddEventLog(eventLogSettings);
});

The crucial part is setting the log level with builder.SetMinimumLevel to the lowest level possible and doing the filtering within the providers themselves.

NLog.Extension.Logging ver. 1.7.2 now supports the NLogProviderOptions ReplaceLoggerFactory and RemoveLoggerFactoryFilter.

And the goal is to change default-value for RemoveLoggerFactoryFilter=true. See also: https://github.com/NLog/NLog.Extensions.Logging/issues/483

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imanushin picture imanushin  路  3Comments

smeegoan picture smeegoan  路  3Comments

geedsen picture geedsen  路  3Comments

carkov1990 picture carkov1990  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments