示例#1
0
 public LogRotater(RotateType supportedType, LogRotateOptions options)
 {
     if (options.RotateType != supportedType)
     {
         throw new NotSupportedException("only support " + supportedType + " but encounter " + options.RotateType);
     }
     this.options = options;
 }
示例#2
0
 private void BuildRotateType(LogRotateOptions options)
 {
     foreach (var name in Enum.GetNames(typeof(RotateType)))
     {
         string arguments;
         if (parameters.TryGetValue(name, out arguments))
         {
             options.RotateType      = (RotateType)Enum.Parse(typeof(RotateType), name);
             options.RotateArguments = arguments;
         }
     }
 }
示例#3
0
        public virtual LogRotateOptions Build()
        {
            var options = new LogRotateOptions();

            BuildRotateType(options);
            BuildRotate(options);
            BuildCompress(options);
            BuildDelayCompress(options);
            BuildIncludeSubDirs(options);

            BuildRootFilter(options);
            return(options);
        }
示例#4
0
        public LogRotateOptions[] Reconfig()
        {
            var builders = new FileLogRotateOptionsBuilderProvider(optionsFile).CreateBuilders();
            var rotaters = new LogRotater[builders.Length];
            var options  = new LogRotateOptions[builders.Length];

            for (int i = 0; i < builders.Length; i++)
            {
                options[i]  = builders[i].Build();
                rotaters[i] = LogRotater.Create(options[i]);
            }
            this.rotaters = rotaters;
            return(options);
        }
示例#5
0
        private void BuildRootFilter(LogRotateOptions options)
        {
            options.Root   = Path.GetDirectoryName(root);
            options.Filter = Path.GetFileName(root);

            if (string.IsNullOrEmpty(options.Root))
            {
                options.Root = "logs";
            }

            if (string.IsNullOrEmpty(options.Filter))
            {
                options.Filter = "*.log";
            }
        }
示例#6
0
        private void BuildCompress(LogRotateOptions options)
        {
            string compress;

            options.Compress = true;
            if (parameters.TryGetValue("compress", out compress))
            {
                if (compress == "off")
                {
                    options.Compress = false;
                }
                else if (!string.IsNullOrEmpty(compress) && compress != "on")
                {
                    throw new InvalidOperationException("invalid compress value " + compress);
                }
            }
        }
示例#7
0
        private void BuildRotate(LogRotateOptions options)
        {
            string rotate;

            if (parameters.TryGetValue("rotate", out rotate))
            {
                int days;
                if (!int.TryParse(rotate, out days))
                {
                    throw new InvalidOperationException("invalid rotate value " + rotate);
                }
                options.Rotate = days;
            }
            else
            {
                options.Rotate = 90;
            }
        }
示例#8
0
        private void BuildDelayCompress(LogRotateOptions options)
        {
            string delaycompress;
            int    days = 1;

            if (parameters.TryGetValue("delaycompress", out delaycompress))
            {
                if (!string.IsNullOrEmpty(delaycompress) &&
                    !int.TryParse(delaycompress, out days))
                {
                    throw new InvalidOperationException("invalid delaycompress value " + delaycompress);
                }

                if (days < 0)
                {
                    throw new InvalidOperationException("delaycompress out of range 0.");
                }
            }
            options.DelayCompress = days;
        }
示例#9
0
        public static LogRotater Create(LogRotateOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            switch (options.RotateType)
            {
            case RotateType.Minutely: return(new MinutelyLogRotater(options));

            case RotateType.Hourly: return(new HourlyLogRotater(options));

            case RotateType.Daily: return(new DailyLogRotater(options));

            case RotateType.Weekly: return(new WeeklyLogRotater(options));

            case RotateType.Monthly: return(new MonthlyLogRotater(options));

            case RotateType.Yearly: return(new YearlyLogRotater(options));
            }
            throw new NotSupportedException(options.RotateType.ToString());
        }
示例#10
0
 public MinutelyLogRotater(LogRotateOptions options) : base(RotateType.Minutely, options)
 {
     this.time = options.RotateArguments ?? "0";
 }
示例#11
0
 private void BuildIncludeSubDirs(LogRotateOptions options)
 {
     options.IncludeSubDirs = parameters.ContainsKey("includesubdirs");
 }
示例#12
0
 public YearlyLogRotater(LogRotateOptions options) : base(RotateType.Yearly, options)
 {
     this.time = options.RotateArguments ?? "1.1";
 }
示例#13
0
 public MonthlyLogRotater(LogRotateOptions options) : base(RotateType.Monthly, options)
 {
     this.time = options.RotateArguments ?? "1";
 }
示例#14
0
 public DailyLogRotater(LogRotateOptions options) : base(RotateType.Daily, options)
 {
     this.time = options.RotateArguments ?? "0:00:00";
 }