Azimuth
Azimuth

Reputation: 2698

Force log4net logging level

It turns out that one of my colleagues has disabled the logging level check in the source code:

private static void Log(string source, EnumLogType logType, string msg)
    {
        ....

        switch (logType)
        {
            case EnumLogType.ERROR:
                //if (log.IsErrorEnabled) { log.Error(logMsg); }
                log.Error(logMsg);
                break;
            case EnumLogType.WARNING:
                //if (log.IsWarnEnabled) { log.Warn(logMsg); }
                log.Warn(logMsg); 
                break;
            case EnumLogType.INFO:
                //if (log.IsInfoEnabled) { log.Info(logMsg); }
                log.Info(logMsg); 
                break;
            case EnumLogType.VERBOSE:
                //if (log.IsDebugEnabled) { log.Debug(logMsg); }
                log.Debug(logMsg); 
                break;
            default:
                log.Debug(logMsg);
                break;
        }
    }

So our product is now writing debug logs even though the level in the configuration file is set to ERROR.

Is there any way to force log4net to log only Error messages? In other words, can I force log4net to check the logging level by configuring it and without editing and recompiling the source code?

Upvotes: 1

Views: 1565

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273711

Your colleague only disabled your own EnumLogType level settings.
The log4net logging level should still work.

In your AppName.Exe.config or Web.Config, use something like:

<log4net>
    ...
    <logger name="...">
      ...
      <level value="Error" />
    </logger>
</log4net>

Upvotes: 2

MattC
MattC

Reputation: 4014

What appenders do you use? If it's a DatabaseAppender you could try and alter the sql command text.

Or you could edit the log4net.config file to only apply certain appenders for certain loglevels, using the filter section.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="{LogName}" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="[%date] [%level] [%thread] [%logger] [%P{userLoginName}] [%P{HostName}] [%P{IPAddress}] [%P{methodName}] [%message] [%exception]%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO" />
      <levelMax value="WARN" />
    </filter>
  </appender>

Upvotes: 1

Related Questions