log4net 运行时改变日志级别

((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);

 其它-未测试

public static void ModifyAppenders<T>(this ILoggerRepository repository, Action<T> modify) where T:log4net.Appender.AppenderSkeleton
{
    var appenders = from appender in log4net.LogManager.GetRepository().GetAppenders()
                        where appender is T
                        select appender as T;
 
    foreach (var appender in appenders)
    {
        modify(appender);
        appender.ActivateOptions();
    }
}

log4net.LogManager.GetRepository().ModifyAppenders<FileAppender>(a =>
                {
                    a.File = Path.Combine(settings.ConfigDirectory,
                                          Path.GetFileName(a.File));
                });

 

 private void TurnOnLogging()
        {
            
            log4net.Repository.ILoggerRepository[] repositories= log4net.LogManager.GetAllRepositories();

            //Configure all loggers to be at the debug level.
            foreach (log4net.Repository.ILoggerRepository repository in repositories)
            {
                repository.Threshold = repository.LevelMap["DEBUG"];
                log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)repository;
                log4net.Core.ILogger[] loggers=hier.GetCurrentLoggers();
                foreach (log4net.Core.ILogger logger in loggers)
                {
                    ((log4net.Repository.Hierarchy.Logger) logger).Level = hier.LevelMap["DEBUG"];
                }
            }

            //Configure the root logger.
            log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository();
            log4net.Repository.Hierarchy.Logger rootLogger = h.Root;
            rootLogger.Level = h.LevelMap["DEBUG"];

        }

 

posted @ 2015-07-29 16:12  真幻de现实  阅读(1456)  评论(0编辑  收藏  举报