diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs index 39f3b27..c66998f 100644 --- a/ExtremelySimpleLogger/FileSink.cs +++ b/ExtremelySimpleLogger/FileSink.cs @@ -6,15 +6,18 @@ namespace ExtremelySimpleLogger { /// public class FileSink : Sink { + private readonly FileInfo file; private readonly StreamWriter writer; + private readonly bool reopenOnWrite; /// /// Creates a new file sink with the given settings. /// /// The full, or relative, path of the file to write to /// Whether new output should be appended to the old log file - public FileSink(string file, bool append) : - this(new FileInfo(file), append) { + /// 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) { } /// @@ -22,7 +25,11 @@ namespace ExtremelySimpleLogger { /// /// The full, or relative, path of the file to write to /// Whether new output should be appended to the old log file - public FileSink(FileInfo file, bool append) { + /// 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) { + this.reopenOnWrite = reopenOnWrite; + this.file = file; + var dir = file.Directory; if (dir != null && !dir.Exists) dir.Create(); @@ -30,8 +37,10 @@ namespace ExtremelySimpleLogger { if (!append && file.Exists) file.Delete(); - this.writer = file.AppendText(); - this.writer.AutoFlush = true; + if (!reopenOnWrite) { + this.writer = file.AppendText(); + this.writer.AutoFlush = true; + } } /// @@ -39,7 +48,12 @@ namespace ExtremelySimpleLogger { /// /// The message to log public override void Log(string s) { - this.writer.WriteLine(s); + if (this.reopenOnWrite) { + using (var w = this.file.AppendText()) + w.WriteLine(s); + } else { + this.writer.WriteLine(s); + } } }