expose some more information in file, console and directory sinks

This commit is contained in:
Ell 2022-01-17 12:49:50 +01:00
parent 5072fde64b
commit 01b5acff73
4 changed files with 45 additions and 16 deletions

View file

@ -9,9 +9,9 @@ namespace ExtremelySimpleLogger {
/// <summary> /// <summary>
/// The <see cref="ConsoleColors"/> that each <see cref="LogLevel"/> is displayed with using this console sink. /// The <see cref="ConsoleColors"/> that each <see cref="LogLevel"/> is displayed with using this console sink.
/// To edit and query this collection, use <see cref="SetColor"/> and <see cref="GetColor"/>. /// To edit and query this collection, you can also use <see cref="SetColor"/> and <see cref="GetColor"/>.
/// </summary> /// </summary>
protected readonly Dictionary<LogLevel, ConsoleColor> ConsoleColors = new Dictionary<LogLevel, ConsoleColor> { public readonly Dictionary<LogLevel, ConsoleColor> ConsoleColors = new Dictionary<LogLevel, ConsoleColor> {
{LogLevel.Warn, ConsoleColor.DarkYellow}, {LogLevel.Warn, ConsoleColor.DarkYellow},
{LogLevel.Error, ConsoleColor.DarkRed}, {LogLevel.Error, ConsoleColor.DarkRed},
{LogLevel.Fatal, ConsoleColor.DarkRed} {LogLevel.Fatal, ConsoleColor.DarkRed}

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -10,6 +11,26 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
public class DirectorySink : Sink { public class DirectorySink : Sink {
/// <summary>
/// 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 <see cref="CurrentFile"/>.
/// </summary>
public readonly IList<FileInfo> OldFiles;
/// <summary>
/// The <see cref="DirectoryInfo"/> that this sink is currently using as its destination to store the <see cref="OldFiles"/> and <see cref="CurrentFile"/>.
/// </summary>
public readonly DirectoryInfo Directory;
/// <summary>
/// The <see cref="FileInfo"/> that this sink is currently using as its destination.
/// </summary>
public FileInfo CurrentFile {
get {
lock (this.file)
return this.file;
}
}
private const string DefaultDateFormat = "yy-MM-dd_HH-mm-ss"; private const string DefaultDateFormat = "yy-MM-dd_HH-mm-ss";
private readonly FileInfo file; private readonly FileInfo file;
private readonly StreamWriter writer; private readonly StreamWriter writer;
@ -23,8 +44,7 @@ namespace ExtremelySimpleLogger {
/// <param name="reopenOnWrite">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.</param> /// <param name="reopenOnWrite">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.</param>
/// <param name="dateFormat">The way the name of the current log file gets formatted. <code>yy-MM-dd_HH-mm-ss</code> by default.</param> /// <param name="dateFormat">The way the name of the current log file gets formatted. <code>yy-MM-dd_HH-mm-ss</code> by default.</param>
public DirectorySink(string directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) : 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) {}
}
/// <summary> /// <summary>
/// Creates a new directory sink with the given settings. /// Creates a new directory sink with the given settings.
@ -35,6 +55,7 @@ namespace ExtremelySimpleLogger {
/// <param name="dateFormat">The way the name of the current log file gets formatted. <code>yy-MM-dd_HH-mm-ss</code> by default.</param> /// <param name="dateFormat">The way the name of the current log file gets formatted. <code>yy-MM-dd_HH-mm-ss</code> by default.</param>
public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) { public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) {
this.reopenOnWrite = reopenOnWrite; this.reopenOnWrite = reopenOnWrite;
this.Directory = directory;
try { try {
if (!directory.Exists) if (!directory.Exists)
@ -44,16 +65,13 @@ namespace ExtremelySimpleLogger {
} }
try { try {
// delete old files // delete files in order of creation time so that older files are deleted first
var files = directory.GetFiles(); var ordered = directory.EnumerateFiles().OrderBy(f => f.CreationTime).ToList();
if (files.Length >= maxFiles) { while (ordered.Count >= maxFiles) {
// order files by their creation time so that older files are deleted first ordered[0].Delete();
var ordered = files.OrderBy(f => f.CreationTime).ToList(); ordered.RemoveAt(0);
while (ordered.Count >= maxFiles) {
ordered[0].Delete();
ordered.RemoveAt(0);
}
} }
this.OldFiles = ordered.AsReadOnly();
} catch (Exception e) { } catch (Exception e) {
throw new IOException($"Failed to delete old files in directory sink {directory}", e); throw new IOException($"Failed to delete old files in directory sink {directory}", e);
} }

View file

@ -7,6 +7,16 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
public class FileSink : Sink { public class FileSink : Sink {
/// <summary>
/// The <see cref="FileInfo"/> that this sink is currently using as its destination.
/// </summary>
public FileInfo CurrentFile {
get {
lock (this.file)
return this.file;
}
}
private const int OneGb = 1024 * 1024 * 1024; private const int OneGb = 1024 * 1024 * 1024;
private readonly FileInfo file; private readonly FileInfo file;
private readonly StreamWriter writer; private readonly StreamWriter writer;
@ -20,8 +30,7 @@ namespace ExtremelySimpleLogger {
/// <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> /// <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>
/// <param name="fileSizeLimit">If <paramref name="append"/> is true, this property determines how big the log file has to be (in bytes) before it is deleted on startup. Defaults to 1gb.</param> /// <param name="fileSizeLimit">If <paramref name="append"/> is true, this property determines how big the log file has to be (in bytes) before it is deleted on startup. Defaults to 1gb.</param>
public FileSink(string file, bool append, bool reopenOnWrite = false, int fileSizeLimit = OneGb) : 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) {}
}
/// <summary> /// <summary>
/// Creates a new file sink with the given settings. /// Creates a new file sink with the given settings.

View file

@ -12,7 +12,9 @@ namespace Sample {
var sinks = new List<Sink> { var sinks = new List<Sink> {
new FileSink("Log.txt", true), new FileSink("Log.txt", true),
// We only want to log messages with a higher importance in the console // 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 { var logger = new Logger {
Name = "Example Logger", Name = "Example Logger",