Beispiel #1
0
        /// <summary>
        /// 获取日志设置
        /// </summary>
        public static ILog GetCustomLogger(string loggerName, string loggerSource, string category, bool additivity = false)
        {
            return(loggerContainer.GetOrAdd(loggerName + (category ?? "") + loggerSource, delegate(string name)
            {
                RollingFileAppender newAppender = null;
                TWReadParameterAppender appender = null;

                if (appenderContainer.ContainsKey(loggerName))
                {
                    appender = appenderContainer[loggerName];

                    // 判断日志路径
                    string filePath = appender.File;
                    if (filePath != null && filePath.EndsWith("\\"))
                    {
                        if (string.IsNullOrEmpty(category))
                        {
                            filePath = string.Format(@"{0}\{1}\{2}\{3}\{4}\{5}.txt", filePath, loggerName, loggerSource,
                                                     DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.ToString().PadLeft(2, '0'));
                        }
                        else
                        {
                            filePath = string.Format(@"{0}\{1}\{2}\{3}\{4}\{5}\{6}.txt", filePath, loggerName, category, loggerSource,
                                                     DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.ToString().PadLeft(2, '0'));
                        }
                    }

                    newAppender = GetNewFileApender(loggerName,
                                                    filePath,
                                                    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();
                log4net.Repository.Hierarchy.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);
            }));
        }
Beispiel #2
0
        /// <summary>
        /// 读取配置文件并缓存
        /// </summary>
        static TWRollingFileLogger()
        {
            Uri uri = new Uri(Common.AssemblyFullPath + ".config");

            log4net.Config.XmlConfigurator.Configure(uri);
            IAppender[] appenders = LogManager.GetRepository().GetAppenders();

            for (int i = 0; i < appenders.Length; i++)
            {
                if (appenders[i] is TWReadParameterAppender)
                {
                    TWReadParameterAppender appender = (TWReadParameterAppender)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;
                    }
                }
            }
        }