mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-25 11:08:34 +01:00
Compare commits
No commits in common. "b80fa8cd37884ce0e388727065746e645cffc032" and "5072fde64b509bd45bbad4a92adc49eeb7592fe2" have entirely different histories.
b80fa8cd37
...
5072fde64b
5 changed files with 17 additions and 46 deletions
|
@ -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, you can also use <see cref="SetColor"/> and <see cref="GetColor"/>.
|
/// To edit and query this collection, use <see cref="SetColor"/> and <see cref="GetColor"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly Dictionary<LogLevel, ConsoleColor> ConsoleColors = new Dictionary<LogLevel, ConsoleColor> {
|
protected 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}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
@ -11,26 +10,6 @@ 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;
|
||||||
|
@ -44,7 +23,8 @@ 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.
|
||||||
|
@ -55,7 +35,6 @@ 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)
|
||||||
|
@ -65,13 +44,16 @@ namespace ExtremelySimpleLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// delete files in order of creation time so that older files are deleted first
|
// delete old files
|
||||||
var ordered = directory.EnumerateFiles().OrderBy(f => f.CreationTime).ToList();
|
var files = directory.GetFiles();
|
||||||
while (ordered.Count >= maxFiles) {
|
if (files.Length >= maxFiles) {
|
||||||
ordered[0].Delete();
|
// order files by their creation time so that older files are deleted first
|
||||||
ordered.RemoveAt(0);
|
var ordered = files.OrderBy(f => f.CreationTime).ToList();
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<PackageIcon>Logo.png</PackageIcon>
|
<PackageIcon>Logo.png</PackageIcon>
|
||||||
<VersionPrefix>1.2.5</VersionPrefix>
|
<VersionPrefix>1.2.4</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -7,16 +7,6 @@ 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;
|
||||||
|
@ -30,7 +20,8 @@ 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.
|
||||||
|
|
|
@ -12,9 +12,7 @@ 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",
|
||||||
|
|
Loading…
Reference in a new issue