Exemplo n.º 1
0
        private static log4net.Filter.LevelRangeFilter GetLevelRangeFilter(AppenderSkeleton appender)
        {
            log4net.Filter.LevelRangeFilter rangeFilter = null;

            lock (appender)
            {
                log4net.Filter.IFilter filterHead = appender.FilterHead;

                //process through the filters until a level range filter is found
                while (filterHead != null)
                {
                    if (typeof(log4net.Filter.LevelRangeFilter).Equals(filterHead.GetType()))
                    {
                        rangeFilter = filterHead as log4net.Filter.LevelRangeFilter;
                        break;
                    }

                    filterHead = filterHead.Next;
                }
            }
            return(rangeFilter);
        }
Exemplo n.º 2
0
        public static void SeparateOut(Type type)
        {
            if (type == null || separatedLoggers.ContainsKey(type))
            {
                return;
            }

            ILoggerRepository repository = LogManager.GetRepository();

            if (repository == null)
            {
                return;
            }

            IEnumerable <AppenderSkeleton> appenders = repository.GetAppenders().OfType <AppenderSkeleton>();

            if (appenders == null)
            {
                return;
            }

            ILog iLog = LogManager.GetLogger(type);

            separatedLoggers.Add(type, iLog);

            log4net.Filter.LoggerMatchFilter filter = new log4net.Filter.LoggerMatchFilter(),
                                             defaultLoggerFilter = new log4net.Filter.LoggerMatchFilter();
            filter.LoggerToMatch = iLog.Logger.Name;
            defaultLoggerFilter.AcceptOnMatch = filter.AcceptOnMatch = true;

            defaultLoggerFilter.LoggerToMatch = logger.Logger.Name;

            foreach (AppenderSkeleton appender in appenders)
            {
                List <log4net.Filter.IFilter> filters = new List <log4net.Filter.IFilter>();

                log4net.Filter.IFilter current = appender.FilterHead;

                if (current != null)
                {
                    filters.Add(current);
                    while ((current = current.Next) != null)
                    {
                        filters.Add(current);
                    }
                }

                appender.ClearFilters();
                appender.AddFilter(defaultLoggerFilter);

                foreach (log4net.Filter.IFilter f in filters)
                {
                    appender.AddFilter(f);
                }
            }

            RollingFileAppender separateAppender = new RollingFileAppender();

            separateAppender.File = Path.Combine(
                GlobalContext.Properties[Constants.Configuration.Logging.PATH_PROPERTY_NAME].ToNullSafeString(),
                type.FullName.Replace("+", "_") + ".log");
            separateAppender.AppendToFile       = true;
            separateAppender.SecurityContext    = log4net.Core.SecurityContextProvider.DefaultProvider.CreateSecurityContext(separateAppender);
            separateAppender.RollingStyle       = RollingFileAppender.RollingMode.Size;
            separateAppender.DatePattern        = ".yyyyMMdd";
            separateAppender.CountDirection     = 1;
            separateAppender.MaxSizeRollBackups = 7;
            separateAppender.MaximumFileSize    = "4MB";
            separateAppender.AddFilter(filter);
            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout();
            layout.Header            = "Date|Host|Environment|Thread|Level|Message\r\n";
            layout.ConversionPattern = "%date|%property{log4net:HostName}|%2thread|%-5level|%message%newline";
            separateAppender.Layout  = layout;
            log4net.Repository.Hierarchy.Hierarchy hierarchy = repository as log4net.Repository.Hierarchy.Hierarchy;
            hierarchy.Root.AddAppender(separateAppender);
            hierarchy.RaiseConfigurationChanged(EventArgs.Empty);
        }