mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-22 10:03:29 +01:00
allowed file sinks to reopen the file every time they write to it
This commit is contained in:
parent
dd62d3d2c1
commit
a518c5c674
1 changed files with 20 additions and 6 deletions
|
@ -6,15 +6,18 @@ namespace ExtremelySimpleLogger {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class FileSink : Sink {
|
public class FileSink : Sink {
|
||||||
|
|
||||||
|
private readonly FileInfo file;
|
||||||
private readonly StreamWriter writer;
|
private readonly StreamWriter writer;
|
||||||
|
private readonly bool reopenOnWrite;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new file sink with the given settings.
|
/// Creates a new file sink with the given settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file">The full, or relative, path of the file to write to</param>
|
/// <param name="file">The full, or relative, path of the file to write to</param>
|
||||||
/// <param name="append">Whether new output should be appended to the old log file</param>
|
/// <param name="append">Whether new output should be appended to the old log file</param>
|
||||||
public FileSink(string file, bool append) :
|
/// <param name="reopenOnWrite">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.</param>
|
||||||
this(new FileInfo(file), append) {
|
public FileSink(string file, bool append, bool reopenOnWrite = false) :
|
||||||
|
this(new FileInfo(file), append, reopenOnWrite) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -22,7 +25,11 @@ namespace ExtremelySimpleLogger {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file">The full, or relative, path of the file to write to</param>
|
/// <param name="file">The full, or relative, path of the file to write to</param>
|
||||||
/// <param name="append">Whether new output should be appended to the old log file</param>
|
/// <param name="append">Whether new output should be appended to the old log file</param>
|
||||||
public FileSink(FileInfo file, bool append) {
|
/// <param name="reopenOnWrite">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.</param>
|
||||||
|
public FileSink(FileInfo file, bool append, bool reopenOnWrite = false) {
|
||||||
|
this.reopenOnWrite = reopenOnWrite;
|
||||||
|
this.file = file;
|
||||||
|
|
||||||
var dir = file.Directory;
|
var dir = file.Directory;
|
||||||
if (dir != null && !dir.Exists)
|
if (dir != null && !dir.Exists)
|
||||||
dir.Create();
|
dir.Create();
|
||||||
|
@ -30,8 +37,10 @@ namespace ExtremelySimpleLogger {
|
||||||
if (!append && file.Exists)
|
if (!append && file.Exists)
|
||||||
file.Delete();
|
file.Delete();
|
||||||
|
|
||||||
this.writer = file.AppendText();
|
if (!reopenOnWrite) {
|
||||||
this.writer.AutoFlush = true;
|
this.writer = file.AppendText();
|
||||||
|
this.writer.AutoFlush = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -39,7 +48,12 @@ namespace ExtremelySimpleLogger {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="s">The message to log</param>
|
/// <param name="s">The message to log</param>
|
||||||
public override void Log(string s) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue