From 01b5acff738b8f6956e888bd36a5cd8cb41aa040 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 17 Jan 2022 12:49:50 +0100 Subject: [PATCH] expose some more information in file, console and directory sinks --- ExtremelySimpleLogger/ConsoleSink.cs | 4 +-- ExtremelySimpleLogger/DirectorySink.cs | 40 +++++++++++++++++++------- ExtremelySimpleLogger/FileSink.cs | 13 +++++++-- Sample/Program.cs | 4 ++- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/ExtremelySimpleLogger/ConsoleSink.cs b/ExtremelySimpleLogger/ConsoleSink.cs index c301712..6d5aa53 100644 --- a/ExtremelySimpleLogger/ConsoleSink.cs +++ b/ExtremelySimpleLogger/ConsoleSink.cs @@ -9,9 +9,9 @@ namespace ExtremelySimpleLogger { /// /// The that each is displayed with using this console sink. - /// To edit and query this collection, use and . + /// To edit and query this collection, you can also use and . /// - protected readonly Dictionary ConsoleColors = new Dictionary { + public readonly Dictionary ConsoleColors = new Dictionary { {LogLevel.Warn, ConsoleColor.DarkYellow}, {LogLevel.Error, ConsoleColor.DarkRed}, {LogLevel.Fatal, ConsoleColor.DarkRed} diff --git a/ExtremelySimpleLogger/DirectorySink.cs b/ExtremelySimpleLogger/DirectorySink.cs index dd9ab9b..4ca6dcd 100644 --- a/ExtremelySimpleLogger/DirectorySink.cs +++ b/ExtremelySimpleLogger/DirectorySink.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; @@ -10,6 +11,26 @@ namespace ExtremelySimpleLogger { /// public class DirectorySink : Sink { + /// + /// The set of old files that are currently in the directory that this sink is referencing, and that have not been deleted on construction. + /// The files in this list are ordered by creation date in ascending order, meaning that the first entry is the least recently created one. + /// Note that this collection does not contain the . + /// + public readonly IList OldFiles; + /// + /// The that this sink is currently using as its destination to store the and . + /// + public readonly DirectoryInfo Directory; + /// + /// The that this sink is currently using as its destination. + /// + public FileInfo CurrentFile { + get { + lock (this.file) + return this.file; + } + } + private const string DefaultDateFormat = "yy-MM-dd_HH-mm-ss"; private readonly FileInfo file; private readonly StreamWriter writer; @@ -23,8 +44,7 @@ namespace ExtremelySimpleLogger { /// Whether this sink should reopen the file every time it logs to it. If this is false, the file will be kept open by this sink. /// The way the name of the current log file gets formatted. yy-MM-dd_HH-mm-ss by default. public DirectorySink(string directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) : - this(new DirectoryInfo(directory), maxFiles, reopenOnWrite, dateFormat) { - } + this(new DirectoryInfo(directory), maxFiles, reopenOnWrite, dateFormat) {} /// /// Creates a new directory sink with the given settings. @@ -35,6 +55,7 @@ namespace ExtremelySimpleLogger { /// The way the name of the current log file gets formatted. yy-MM-dd_HH-mm-ss by default. public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) { this.reopenOnWrite = reopenOnWrite; + this.Directory = directory; try { if (!directory.Exists) @@ -44,16 +65,13 @@ namespace ExtremelySimpleLogger { } try { - // delete old files - var files = directory.GetFiles(); - if (files.Length >= maxFiles) { - // order files by their creation time so that older files are deleted first - var ordered = files.OrderBy(f => f.CreationTime).ToList(); - while (ordered.Count >= maxFiles) { - ordered[0].Delete(); - ordered.RemoveAt(0); - } + // delete files in order of creation time so that older files are deleted first + var ordered = directory.EnumerateFiles().OrderBy(f => f.CreationTime).ToList(); + while (ordered.Count >= maxFiles) { + ordered[0].Delete(); + ordered.RemoveAt(0); } + this.OldFiles = ordered.AsReadOnly(); } catch (Exception e) { throw new IOException($"Failed to delete old files in directory sink {directory}", e); } diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs index 7e21189..e708402 100644 --- a/ExtremelySimpleLogger/FileSink.cs +++ b/ExtremelySimpleLogger/FileSink.cs @@ -7,6 +7,16 @@ namespace ExtremelySimpleLogger { /// public class FileSink : Sink { + /// + /// The that this sink is currently using as its destination. + /// + public FileInfo CurrentFile { + get { + lock (this.file) + return this.file; + } + } + private const int OneGb = 1024 * 1024 * 1024; private readonly FileInfo file; private readonly StreamWriter writer; @@ -20,8 +30,7 @@ namespace ExtremelySimpleLogger { /// 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. /// 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) { - } + this(new FileInfo(file), append, reopenOnWrite, fileSizeLimit) {} /// /// Creates a new file sink with the given settings. diff --git a/Sample/Program.cs b/Sample/Program.cs index 5d9543c..e521c86 100644 --- a/Sample/Program.cs +++ b/Sample/Program.cs @@ -12,7 +12,9 @@ namespace Sample { var sinks = new List { new FileSink("Log.txt", true), // We only want to log messages with a higher importance in the console - new ConsoleSink {MinimumLevel = LogLevel.Info} + new ConsoleSink {MinimumLevel = LogLevel.Info}, + // we allow a total of 5 files in our directory sink before old ones start being deleted + new DirectorySink("AllLogs", 5) }; var logger = new Logger { Name = "Example Logger",