mirror of https://github.com/winsw/winsw
roll-by-time now supports keepFiles argument
parent
6cf303c1d3
commit
4b2b6cc636
|
@ -52,7 +52,16 @@ namespace WinSW.Configuration
|
|||
return new RollingLogAppender(this.Directory, this.Name, this.OutFileDisabled, this.ErrFileDisabled, this.OutFilePattern, this.ErrFilePattern);
|
||||
|
||||
case "roll-by-time":
|
||||
return new TimeBasedRollingLogAppender(this.Directory, this.Name, this.OutFileDisabled, this.ErrFileDisabled, this.OutFilePattern, this.ErrFilePattern, this.Pattern, this.Period.GetValueOrDefault(1));
|
||||
return new TimeBasedRollingLogAppender(
|
||||
this.Directory,
|
||||
this.Name,
|
||||
this.OutFileDisabled,
|
||||
this.ErrFileDisabled,
|
||||
this.OutFilePattern,
|
||||
this.ErrFilePattern,
|
||||
this.Pattern,
|
||||
this.Period.GetValueOrDefault(1),
|
||||
this.KeepFiles);
|
||||
|
||||
case "roll-by-size":
|
||||
return new SizeBasedRollingLogAppender(
|
||||
|
@ -104,4 +113,4 @@ namespace WinSW.Configuration
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -351,7 +351,14 @@ namespace WinSW
|
|||
|
||||
public override int? SizeThreshold => this.config.SingleIntElement(this.Element, "sizeThreshold", 10 * 1024);
|
||||
|
||||
public override int? KeepFiles => this.config.SingleIntElement(this.Element, "keepFiles", SizeBasedRollingLogAppender.DefaultFilesToKeep);
|
||||
public override int? KeepFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
var keepFiles = this.Element.SelectSingleNode("keepFiles");
|
||||
return keepFiles is null ? null : int.Parse(keepFiles.InnerText);
|
||||
}
|
||||
}
|
||||
|
||||
public override int? Period => this.config.SingleIntElement(this.Element, "period", 1);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
#if VNEXT
|
||||
using System.IO.Compression;
|
||||
|
@ -85,10 +86,13 @@ namespace WinSW
|
|||
|
||||
protected string ErrFilePattern { get; private set; }
|
||||
|
||||
protected string LogDirectory { get; private set; }
|
||||
|
||||
protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern)
|
||||
: base(outFileDisabled, errFileDisabled)
|
||||
{
|
||||
this.BaseLogFileName = Path.Combine(logDirectory, baseName);
|
||||
this.LogDirectory = logDirectory;
|
||||
this.OutFilePattern = outFilePattern;
|
||||
this.ErrFilePattern = errFilePattern;
|
||||
}
|
||||
|
@ -171,15 +175,20 @@ namespace WinSW
|
|||
|
||||
public class TimeBasedRollingLogAppender : AbstractFileLogAppender
|
||||
{
|
||||
public static int? DefaultFilesToKeep = null;
|
||||
|
||||
public string Pattern { get; private set; }
|
||||
|
||||
public int Period { get; private set; }
|
||||
|
||||
public TimeBasedRollingLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern, string pattern, int period)
|
||||
public int? FilesToKeep { get; private set; }
|
||||
|
||||
public TimeBasedRollingLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern, string pattern, int period, int? filesToKeep)
|
||||
: base(logDirectory, baseName, outFileDisabled, errFileDisabled, outFilePattern, errFilePattern)
|
||||
{
|
||||
this.Pattern = pattern;
|
||||
this.Period = period;
|
||||
this.FilesToKeep = filesToKeep;
|
||||
}
|
||||
|
||||
protected override void LogOutput(StreamReader outputReader)
|
||||
|
@ -208,6 +217,40 @@ namespace WinSW
|
|||
{
|
||||
writer.Dispose();
|
||||
copy.Writer = writer = new FileStream(this.BaseLogFileName + "_" + periodicRollingCalendar.Format + ext, FileMode.Create);
|
||||
|
||||
if (this.FilesToKeep != null)
|
||||
{
|
||||
var logFiles = new List<string>();
|
||||
|
||||
foreach (string file in Directory.GetFiles(this.LogDirectory, "*" + ext))
|
||||
{
|
||||
DateTime createdAt = File.GetCreationTime(file);
|
||||
|
||||
if (this.BaseLogFileName + "_" + periodicRollingCalendar.GetFormatForDateTime(createdAt) + ext == file)
|
||||
{
|
||||
logFiles.Add(file);
|
||||
}
|
||||
}
|
||||
|
||||
logFiles.Sort((x, y) => File.GetCreationTime(x).CompareTo(File.GetCreationTime(y)));
|
||||
try
|
||||
{
|
||||
while (this.FilesToKeep < logFiles.Count)
|
||||
{
|
||||
var filename = logFiles[0];
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
logFiles.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
this.EventLogger.LogEvent("Failed to roll log: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -113,6 +113,8 @@ namespace WinSW
|
|||
}
|
||||
}
|
||||
|
||||
public string Format => this.currentRoll.ToString(this.format);
|
||||
public string GetFormatForDateTime(DateTime datetime) => datetime.ToString(this.format);
|
||||
|
||||
public string Format => this.GetFormatForDateTime(this.currentRoll);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue