Esempio n. 1
0
        /// <summary>
        /// GetCustomLogger
        /// </summary>
        /// <param name="loggerName">名称</param>
        /// <param name="category">路径</param>
        /// <param name="additivity">取值是true或false,默认值是true。设置为false时将阻止父logger中的appender</param>
        /// <returns></returns>
        public static ILog GetCustomLogger(string loggerName, string category = null, bool additivity = false)
        {
            loggerName = string.Format("{0:yyyy-MM-dd HH}{1}_{2}", DateTime.Now, "时", loggerName);
            string a = loggerName;

            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);
            }));
        }
Esempio n. 2
0
 /// <summary>
 /// CustomRollingFileLogger
 /// </summary>
 static CustomRollingFileLogger()
 {
     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; //设置无限备份=-1 ,最大备份数为1000
             }
             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;
             }
         }
     }
 }