Improve RollingSizeTimeLogAppender

pull/354/head
NextTurn 2018-12-06 00:00:00 +08:00
parent 8864b676fb
commit bab17429d2
No known key found for this signature in database
GPG Key ID: 17A0D50ADDE1A0C4
1 changed files with 40 additions and 54 deletions

View File

@ -366,7 +366,18 @@ namespace winsw
public int? ZipOlderThanNumDays { get; private set; } public int? ZipOlderThanNumDays { get; private set; }
public string ZipDateFormat { get; private set; } public string ZipDateFormat { get; private set; }
public RollingSizeTimeLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern, int sizeThreshold, string filePattern, TimeSpan? autoRollAtTime, int? zipolderthannumdays, string zipdateformat) public RollingSizeTimeLogAppender(
string logDirectory,
string baseName,
bool outFileDisabled,
bool errFileDisabled,
string outFilePattern,
string errFilePattern,
int sizeThreshold,
string filePattern,
TimeSpan? autoRollAtTime,
int? zipolderthannumdays,
string zipdateformat)
: base(logDirectory, baseName, outFileDisabled, errFileDisabled, outFilePattern, errFilePattern) : base(logDirectory, baseName, outFileDisabled, errFileDisabled, outFilePattern, errFilePattern)
{ {
SizeTheshold = sizeThreshold; SizeTheshold = sizeThreshold;
@ -385,7 +396,7 @@ namespace winsw
new Thread(() => CopyStreamWithRotation(errorStream, ErrFilePattern)).Start(); new Thread(() => CopyStreamWithRotation(errorStream, ErrFilePattern)).Start();
} }
private void CopyStreamWithRotation(Stream data, string ext) private void CopyStreamWithRotation(Stream data, string extension)
{ {
// lock required as the timer thread and the thread that will write to the stream could try and access the file stream at the same time // lock required as the timer thread and the thread that will write to the stream could try and access the file stream at the same time
var fileLock = new object(); var fileLock = new object();
@ -394,7 +405,7 @@ namespace winsw
var baseDirectory = Path.GetDirectoryName(BaseLogFileName); var baseDirectory = Path.GetDirectoryName(BaseLogFileName);
var baseFileName = Path.GetFileName(BaseLogFileName); var baseFileName = Path.GetFileName(BaseLogFileName);
var logFile = string.Format("{0}{1}", BaseLogFileName, ext); var logFile = BaseLogFileName + extension;
var w = new FileStream(logFile, FileMode.Append); var w = new FileStream(logFile, FileMode.Append);
var sz = new FileInfo(logFile).Length; var sz = new FileInfo(logFile).Length;
@ -415,8 +426,8 @@ namespace winsw
w.Close(); w.Close();
var now = DateTime.Now.AddDays(-1); var now = DateTime.Now.AddDays(-1);
var nextFileNumber = GetNextFileNumber(ext, baseDirectory, baseFileName, now); var nextFileNumber = GetNextFileNumber(extension, baseDirectory, baseFileName, now);
var nextFileName = Path.Combine(baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(FilePattern), nextFileNumber, ext)); var nextFileName = Path.Combine(baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(FilePattern), nextFileNumber, extension));
File.Move(logFile, nextFileName); File.Move(logFile, nextFileName);
w = new FileStream(logFile, FileMode.Create); w = new FileStream(logFile, FileMode.Create);
@ -424,7 +435,7 @@ namespace winsw
} }
// Next day so check if file can be zipped // Next day so check if file can be zipped
ZipFiles(baseDirectory, ext, baseFileName); ZipFiles(baseDirectory, extension, baseFileName);
} }
catch (Exception et) catch (Exception et)
{ {
@ -476,10 +487,10 @@ namespace winsw
// rotate file // rotate file
var now = DateTime.Now; var now = DateTime.Now;
var nextFileNumber = GetNextFileNumber(ext, baseDirectory, baseFileName, now); var nextFileNumber = GetNextFileNumber(extension, baseDirectory, baseFileName, now);
var nextFileName = var nextFileName =
Path.Combine(baseDirectory, Path.Combine(baseDirectory,
string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(FilePattern), nextFileNumber, ext)); string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(FilePattern), nextFileNumber, extension));
File.Move(logFile, nextFileName); File.Move(logFile, nextFileName);
// even if the log rotation fails, create a new one, or else // even if the log rotation fails, create a new one, or else
@ -502,44 +513,44 @@ namespace winsw
w.Close(); w.Close();
} }
private void ZipFiles(string path, string fileExt, string baseZipfilename) private void ZipFiles(string directory, string fileExtension, string zipFileBaseName)
{ {
if (ZipOlderThanNumDays == null || !(ZipOlderThanNumDays > 0)) if (ZipOlderThanNumDays == null || !(ZipOlderThanNumDays > 0))
return; return;
try try
{ {
var files = Directory.GetFiles(path, "*" + fileExt); foreach (string path in Directory.GetFiles(directory, "*" + fileExtension))
foreach (var file in files)
{ {
var fi = new FileInfo(file); var fileInfo = new FileInfo(path);
if (fi.LastWriteTimeUtc >= DateTime.UtcNow.AddDays(-ZipOlderThanNumDays.Value)) if (fileInfo.LastWriteTimeUtc >= DateTime.UtcNow.AddDays(-ZipOlderThanNumDays.Value))
continue; continue;
// lets archive this bugger string sourceFileName = Path.GetFileName(path);
ZipTheFile(file, path, fi.LastWriteTimeUtc.ToString(ZipDateFormat), baseZipfilename); string zipFilePattern = fileInfo.LastAccessTimeUtc.ToString(ZipDateFormat);
File.Delete(file); string zipFilePath = Path.Combine(directory, $"{zipFileBaseName}.{zipFilePattern}.zip");
ZipOneFile(path, sourceFileName, zipFilePath);
File.Delete(path);
} }
} }
catch (Exception e) catch (Exception e)
{ {
EventLogger.LogEvent(string.Format("Failed to Zip File. Error {0}", e.Message)); EventLogger.LogEvent($"Failed to Zip files. Error {e.Message}");
} }
} }
#if VNEXT #if VNEXT
private void ZipTheFile(string sourceFilePath, string zipDirectory, string zipFilePattern, string baseZipFileName) private void ZipOneFile(string sourceFilePath, string entryName, string zipFilePath)
{ {
string zipFilePath = Path.Combine(zipDirectory, $"{baseZipFileName}.{zipFilePattern}.zip");
ZipArchive zipArchive = null; ZipArchive zipArchive = null;
try try
{ {
zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Update); zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Update);
string fileName = Path.GetFileName(sourceFilePath); if (zipArchive.GetEntry(entryName) is null)
if (zipArchive.GetEntry(fileName) is null)
{ {
zipArchive.CreateEntryFromFile(sourceFilePath, fileName); zipArchive.CreateEntryFromFile(sourceFilePath, entryName);
} }
} }
catch (Exception e) catch (Exception e)
@ -552,56 +563,31 @@ namespace winsw
} }
} }
#else #else
private void ZipTheFile(string filename, string zipPath, string zipFilePattern, string baseZipfilename) private void ZipOneFile(string sourceFilePath, string entryName, string zipFilePath)
{ {
var zipfilename = Path.Combine(zipPath, $"{baseZipfilename}.{zipFilePattern}.zip");
ZipFile zipFile = null; ZipFile zipFile = null;
bool commited = false;
try try
{ {
if (File.Exists(zipfilename)) zipFile = new ZipFile(File.Open(zipFilePath, FileMode.OpenOrCreate));
{
zipFile = new ZipFile(zipfilename);
TestZipfile(zipFile, zipfilename);
}
else
{
zipFile = ZipFile.Create(zipfilename);
}
zipFile.BeginUpdate(); zipFile.BeginUpdate();
zipFile.NameTransform = new ZipNameTransform(zipPath);
var relFile = Path.GetFileName(filename); if (zipFile.FindEntry(entryName, false) < 0)
if (zipFile.FindEntry(relFile, true) == -1)
{ {
zipFile.Add(filename); zipFile.Add(sourceFilePath, entryName);
} }
zipFile.CommitUpdate(); zipFile.CommitUpdate();
commited = true;
TestZipfile(zipFile, zipfilename);
} }
catch (Exception e) catch (Exception e)
{ {
EventLogger.LogEvent(string.Format("Failed to Zip the File {0}. Error {1}", filename, e.Message)); EventLogger.LogEvent($"Failed to Zip the File {sourceFilePath}. Error {e.Message}");
if (zipFile != null && !commited) zipFile?.AbortUpdate();
zipFile.AbortUpdate();
} }
finally finally
{ {
zipFile?.Close(); zipFile?.Close();
} }
} }
static void TestZipfile(ZipFile zipFile, string zipArchive)
{
var testResult = zipFile.TestArchive(true);
if (!testResult)
{
var em = string.Format("Bad zip file \"{0}\"", zipArchive);
throw new ApplicationException(em);
}
}
#endif #endif
private double SetupRollTimer() private double SetupRollTimer()