检查/获取c#中配置的LogLevel

本文关键字:配置 LogLevel 获取 检查 | 更新日期: 2023-09-27 18:04:18

我们使用log4net配置log4net。config and AssemblyInfo.cs

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

现在我们需要得到配置的LogLevel (LevelRangeFilter)

在log4net

。配置我们已经设置如下:

<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。有没有一个方法返回配置的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;

检查/获取c#中配置的LogLevel

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