log4net - 在C#中检查/获取配置的LogLevel

时间:2014-05-21 09:43:46

标签: c# log4net

我们使用log4net.config和AssemblyInfo.cs配置log4net

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
          "log4netGui.config", Watch = true)]

现在我们需要获取配置的LogLevel(LevelRangeFilter)

在log4net.config中我们设置如下:

<levelMin value="DEBUG" />
<levelMax value="FATAL" />

在c#中我们可以像这样检查每个LogLevel:

Logging.Log.Fatal(string.Format("LogLevelDebug is enabled: {0}", 
      Logging.Log.Logger.IsEnabledFor(log4net.Core.Level.Debug)));

所以我们必须检查每个log4net.Core.Level.xxxxx - 是不是有一个方法可以在一个数组或类似的东西中返回配置的logLevel?

可用级别为:

public static readonly Level Alert;
public static readonly Level All;
public static readonly Level Critical;
public static readonly Level Debug;
public static readonly Level Emergency;
public static readonly Level Error;
public static readonly Level Fatal;
public static readonly Level Fine;
public static readonly Level Finer;
public static readonly Level Finest;
public static readonly Level Info;
public static readonly Level Log4Net_Debug;
public static readonly Level Notice;
public static readonly Level Off;
public static readonly Level Severe;
public static readonly Level Trace;
public static readonly Level Verbose;
public static readonly Level Warn;

1 个答案:

答案 0 :(得分:1)

private readonly List<Level> logLevels = new List<Level>
{
     Level.Alert,
     Level.All,
     Level.Critical,
     Level.Debug,
     Level.Emergency,
     Level.Error,
     Level.Fatal,
     Level.Fine,
     Level.Finer,
     Level.Finest,
     Level.Info,
     Level.Log4Net_Debug,
     Level.Notice,
     Level.Off,
     Level.Severe,
     Level.Trace,
     Level.Verbose,
     Level.Warn
};

public IList<Level> EnabledLogLevels(ILog logger)
{
    List<Level> levels = new List<Level>();

    foreach (Level level in logLevels)
    {
        if (logger.Logger.IsEnabledFor(level))
        {
            levels.Add(level);
        }
    }

    return levels;
}