From 3092a018674012b1a887aa27e2c6c18067dda3db Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 23 Jul 2020 14:10:43 +0200 Subject: [PATCH] give FileSink a maximum amount of bytes before the file is deleted automatically --- ExtremelySimpleLogger/FileSink.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs index d7105b2..8253282 100644 --- a/ExtremelySimpleLogger/FileSink.cs +++ b/ExtremelySimpleLogger/FileSink.cs @@ -6,6 +6,7 @@ namespace ExtremelySimpleLogger { /// public class FileSink : Sink { + private const int OneGb = 1024 * 1024 * 1024; private readonly FileInfo file; private readonly StreamWriter writer; private readonly bool reopenOnWrite; @@ -16,8 +17,9 @@ namespace ExtremelySimpleLogger { /// The full, or relative, path of the file to write to /// Whether new output should be appended to the old log file /// Whether this file sink should reopen the file every time it logs to it. If this is false, the file will be kept open by this sink. - public FileSink(string file, bool append, bool reopenOnWrite = false) : - this(new FileInfo(file), append, reopenOnWrite) { + /// If is true, this property determines how big the log file has to be (in bytes) before it is deleted on startup. Defaults to 1gb. + public FileSink(string file, bool append, bool reopenOnWrite = false, int fileSizeLimit = OneGb) : + this(new FileInfo(file), append, reopenOnWrite, fileSizeLimit) { } /// @@ -26,7 +28,8 @@ namespace ExtremelySimpleLogger { /// The full, or relative, path of the file to write to /// Whether new output should be appended to the old log file /// Whether this file sink should reopen the file every time it logs to it. If this is false, the file will be kept open by this sink. - public FileSink(FileInfo file, bool append, bool reopenOnWrite = false) { + /// If is true, this property determines how big the log file has to be (in bytes) before it is deleted on startup. + public FileSink(FileInfo file, bool append, bool reopenOnWrite = false, int fileSizeLimit = OneGb) { this.reopenOnWrite = reopenOnWrite; this.file = file; @@ -34,7 +37,7 @@ namespace ExtremelySimpleLogger { if (dir != null && !dir.Exists) dir.Create(); - if (!append && file.Exists) + if (file.Exists && (!append || file.Length >= fileSizeLimit)) file.Delete(); if (!reopenOnWrite) {