mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-12-22 05:09:24 +01:00
expose some more information in file, console and directory sinks
This commit is contained in:
parent
5072fde64b
commit
01b5acff73
4 changed files with 45 additions and 16 deletions
|
@ -9,9 +9,9 @@ namespace ExtremelySimpleLogger {
|
|||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
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.Error, ConsoleColor.DarkRed},
|
||||
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
|
@ -10,6 +11,26 @@ namespace ExtremelySimpleLogger {
|
|||
/// </summary>
|
||||
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 readonly FileInfo file;
|
||||
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="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) :
|
||||
this(new DirectoryInfo(directory), maxFiles, reopenOnWrite, dateFormat) {
|
||||
}
|
||||
this(new DirectoryInfo(directory), maxFiles, reopenOnWrite, dateFormat) {}
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,16 @@ namespace ExtremelySimpleLogger {
|
|||
/// </summary>
|
||||
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 readonly FileInfo file;
|
||||
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="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) :
|
||||
this(new FileInfo(file), append, reopenOnWrite, fileSizeLimit) {
|
||||
}
|
||||
this(new FileInfo(file), append, reopenOnWrite, fileSizeLimit) {}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new file sink with the given settings.
|
||||
|
|
|
@ -12,7 +12,9 @@ namespace Sample {
|
|||
var sinks = new List<Sink> {
|
||||
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",
|
||||
|
|
Loading…
Reference in a new issue