public static ILog GetCustomLogger(string loggerName, string category = null, bool additivity = false) { return(loggerContainer.GetOrAdd(loggerName, delegate(string name) { RollingFileAppender newAppender = null; ReadParamAppender appender = null; if (appenderContainer.ContainsKey(loggerName)) { appender = appenderContainer[loggerName]; newAppender = GetNewFileApender(loggerName, string.IsNullOrEmpty(appender.File) ? GetFile(category, loggerName) : appender.File, appender.MaxSizeRollBackups, appender.AppendToFile, true, appender.MaximumFileSize, RollingFileAppender.RollingMode.Composite, appender.DatePattern, appender.LayoutPattern); } else { newAppender = GetNewFileApender(loggerName, GetFile(category, loggerName), MAX_SIZE_ROLL_BACKUPS, true, true, MAXIMUM_FILE_SIZE, RollingFileAppender.RollingMode.Composite, DATE_PATTERN, LAYOUT_PATTERN); } log4net.Repository.Hierarchy.Hierarchy repository = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(); Logger logger = repository.LoggerFactory.CreateLogger(repository, loggerName); logger.Hierarchy = repository; logger.Parent = repository.Root; logger.Level = GetLoggerLevel(appender == null ? LEVEL : appender.Level); logger.Additivity = additivity; logger.AddAppender(newAppender); logger.Repository.Configured = true; return new LogImpl(logger); })); }
//读取配置文件并缓存 static CustomLogManager() { IAppender[] appenders = LogManager.GetRepository().GetAppenders(); for (int i = 0; i < appenders.Length; i++) { if (appenders[i] is ReadParamAppender) { ReadParamAppender appender = (ReadParamAppender)appenders[i]; if (appender.MaxSizeRollBackups == 0) { appender.MaxSizeRollBackups = MAX_SIZE_ROLL_BACKUPS; } if (appender.Layout != null && appender.Layout is log4net.Layout.PatternLayout) { appender.LayoutPattern = ((log4net.Layout.PatternLayout)appender.Layout).ConversionPattern; } if (string.IsNullOrEmpty(appender.LayoutPattern)) { appender.LayoutPattern = LAYOUT_PATTERN; } if (string.IsNullOrEmpty(appender.DatePattern)) { appender.DatePattern = DATE_PATTERN; } if (string.IsNullOrEmpty(appender.MaximumFileSize)) { appender.MaximumFileSize = MAXIMUM_FILE_SIZE; } if (string.IsNullOrEmpty(appender.Level)) { appender.Level = LEVEL; } lock (lockObj) { appenderContainer[appenders[i].Name] = appender; } } } }