Exemplo n.º 1
0
 public MonthlyLogRotater(LogRotateOptions options) : base(RotateType.Monthly, options)
 {
     if (string.IsNullOrEmpty(options.RotateArguments))
     {
         times = new DateTime[] { DateTime.MinValue };
     }
     else
     {
         var args = options.RotateArguments.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
         times = new DateTime[args.Length];
         for (int i = 0; i < times.Length; i++)
         {
             if (!DateTime.TryParseExact(args[i].Trim(), "d H:m:s", null, DateTimeStyles.None, out times[i]) &&
                 !DateTime.TryParseExact(args[i].Trim(), "d H:m", null, DateTimeStyles.None, out times[i]))
             {
                 int d;
                 if (!int.TryParse(args[i].Trim(), out d) || d > 31)
                 {
                     throw new NotSupportedException("invalid arguments: " + options.RotateArguments);
                 }
                 times[i] = new DateTime(0, 0, d);
             }
         }
     }
 }
Exemplo n.º 2
0
 public LogRotater(RotateType supportedType, LogRotateOptions options)
 {
     if (options.RotateType != supportedType)
     {
         throw new NotSupportedException("only support " + supportedType + " but encounter " + options.RotateType);
     }
     this.options = options;
 }
Exemplo n.º 3
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;
         }
     }
 }
Exemplo n.º 4
0
        private void BuildScriptTimeout(LogRotateOptions options)
        {
            string timeout;
            var    span = TimeSpan.FromHours(6);

            if (parameters.TryGetValue("scripttimeout", out timeout) &&
                !TimeSpan.TryParse(timeout, out span))
            {
                throw new InvalidOperationException("invalid script timeout value " + timeout);
            }
            options.ScriptTimeout = span;
        }
Exemplo n.º 5
0
        private void BuildRootFilter(LogRotateOptions options)
        {
            options.Root   = Path.GetDirectoryName(root);
            options.Filter = Path.GetFileName(root);

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

            if (string.IsNullOrEmpty(options.Filter))
            {
                options.Filter = "*.log";
            }
        }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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;
            }
        }
Exemplo n.º 8
0
        public virtual LogRotateOptions Build()
        {
            var options = new LogRotateOptions();

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

            BuildRootFilter(options);
            BuildScriptTimeout(options);

            options.PreScripts  = preScripts.ToArray();
            options.PostScripts = postScripts.ToArray();

            return(options);
        }
Exemplo n.º 9
0
 public MinutelyLogRotater(LogRotateOptions options) : base(RotateType.Minutely, options)
 {
     if (string.IsNullOrEmpty(options.RotateArguments))
     {
         seconds = new int[0];
     }
     else
     {
         var args = options.RotateArguments.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
         seconds = new int[args.Length];
         for (int i = 0; i < seconds.Length; i++)
         {
             if (!int.TryParse(args[i].Trim(), out seconds[i]) || seconds[i] >= 60)
             {
                 throw new NotSupportedException("invalid arguments: " + options.RotateArguments);
             }
         }
     }
 }
Exemplo n.º 10
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;
        }
Exemplo n.º 11
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());
        }
Exemplo n.º 12
0
 public WeeklyLogRotater(LogRotateOptions options) : base(RotateType.Weekly, options)
 {
     if (string.IsNullOrEmpty(options.RotateArguments))
     {
         times = new DateTime[] { DateTime.MinValue };
     }
     else
     {
         var args = options.RotateArguments.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
         times = new DateTime[args.Length];
         for (int i = 0; i < times.Length; i++)
         {
             if (!DateTime.TryParseExact(args[i].Trim(), "d H:m:s", null, DateTimeStyles.None, out times[i]) &&
                 !DateTime.TryParseExact(args[i].Trim(), "d H:m", null, DateTimeStyles.None, out times[i]))
             {
                 throw new NotSupportedException("invalid arguments: " + options.RotateArguments);
             }
             else if (times[i].Day >= 7)
             {
                 throw new InvalidOperationException("valid date is 1 - 7");
             }
         }
     }
 }
Exemplo n.º 13
0
 private void BuildIncludeSubDirs(LogRotateOptions options)
 {
     options.IncludeSubDirs = parameters.ContainsKey("includesubdirs");
 }