简体   繁体   English

强制log4net日志记录级别

[英]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. 所以我们的产品现在正在编写调试日志,即使配置文件中的级别设置为ERROR。

Is there any way to force log4net to log only Error messages? 有没有办法强制log4net只记录错误消息? In other words, can I force log4net to check the logging level by configuring it and without editing and recompiling the source code? 换句话说,我可以强制log4net通过配置它来检查日志记录级别,而无需编辑和重新编译源代码吗?

Your colleague only disabled your own EnumLogType level settings. 您的同事仅禁用了您自己的EnumLogType级别设置。
The log4net logging level should still work. log4net日志记录级别仍然有效。

In your AppName.Exe.config or Web.Config, use something like: 在AppName.Exe.config或Web.Config中,使用以下内容:

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

What appenders do you use? 你用什么appender? If it's a DatabaseAppender you could try and alter the sql command text. 如果它是DatabaseAppender,您可以尝试更改sql命令文本。

Or you could edit the log4net.config file to only apply certain appenders for certain loglevels, using the filter section. 或者,您可以编辑log4net.config文件,仅使用过滤器部分为某些日志级别应用某些appender。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM