Compare commits

...

5 commits

6 changed files with 78 additions and 26 deletions

View file

@ -36,24 +36,32 @@ namespace ExtremelySimpleLogger {
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;
if (!directory.Exists) try {
directory.Create(); if (!directory.Exists)
directory.Create();
} catch (Exception e) {
throw new IOException($"Failed to create directory sink directory {directory}", e);
}
// delete old files try {
var files = directory.GetFiles(); // delete old files
if (files.Length >= maxFiles) { var files = directory.GetFiles();
// order files by their creation time so that older files are deleted first if (files.Length >= maxFiles) {
var ordered = files.OrderBy(f => f.CreationTime).ToList(); // order files by their creation time so that older files are deleted first
while (ordered.Count >= maxFiles) { var ordered = files.OrderBy(f => f.CreationTime).ToList();
ordered[0].Delete(); while (ordered.Count >= maxFiles) {
ordered.RemoveAt(0); ordered[0].Delete();
ordered.RemoveAt(0);
}
} }
} catch (Exception e) {
throw new IOException($"Failed to delete old files in directory sink {directory}", e);
} }
var date = DateTime.Now.ToString(dateFormat); var date = DateTime.Now.ToString(dateFormat);
this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt")); this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt"));
if (!reopenOnWrite) { if (!reopenOnWrite) {
this.writer = this.file.AppendText(); this.writer = this.Append();
this.writer.AutoFlush = true; this.writer.AutoFlush = true;
} }
} }
@ -67,7 +75,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) { protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) { lock (this.file) {
if (this.reopenOnWrite) { if (this.reopenOnWrite) {
using (var w = this.file.AppendText()) using (var w = this.Append())
w.WriteLine(s); w.WriteLine(s);
} else { } else {
this.writer.WriteLine(s); this.writer.WriteLine(s);
@ -86,5 +94,13 @@ namespace ExtremelySimpleLogger {
} }
} }
private StreamWriter Append() {
try {
return this.file.AppendText();
} catch (Exception e) {
throw new IOException($"Failed to append to directory sink file {this.file}", e);
}
}
} }
} }

View file

@ -12,6 +12,13 @@
<PackageProjectUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</PackageProjectUrl> <PackageProjectUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</RepositoryUrl> <RepositoryUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<VersionPrefix>1.2.3</VersionPrefix> <PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.2.4</VersionPrefix>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
<None Include="../Logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project> </Project>

View file

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
namespace ExtremelySimpleLogger { namespace ExtremelySimpleLogger {
@ -33,15 +34,23 @@ namespace ExtremelySimpleLogger {
this.reopenOnWrite = reopenOnWrite; this.reopenOnWrite = reopenOnWrite;
this.file = file; this.file = file;
var dir = file.Directory; try {
if (dir != null && !dir.Exists) var dir = file.Directory;
dir.Create(); if (dir != null && !dir.Exists)
dir.Create();
} catch (Exception e) {
throw new IOException($"Failed to create directory for file sink {file}", e);
}
if (file.Exists && (!append || file.Length >= fileSizeLimit)) try {
file.Delete(); if (file.Exists && (!append || file.Length >= fileSizeLimit))
file.Delete();
} catch (Exception e) {
throw new IOException($"Failed to delete file sink file {file}", e);
}
if (!reopenOnWrite) { if (!reopenOnWrite) {
this.writer = file.AppendText(); this.writer = this.Append();
this.writer.AutoFlush = true; this.writer.AutoFlush = true;
} }
} }
@ -55,7 +64,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) { protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) { lock (this.file) {
if (this.reopenOnWrite) { if (this.reopenOnWrite) {
using (var w = this.file.AppendText()) using (var w = this.Append())
w.WriteLine(s); w.WriteLine(s);
} else { } else {
this.writer.WriteLine(s); this.writer.WriteLine(s);
@ -74,5 +83,13 @@ namespace ExtremelySimpleLogger {
} }
} }
private StreamWriter Append() {
try {
return this.file.AppendText();
} catch (Exception e) {
throw new IOException($"Failed to append to file sink {this.file}", e);
}
}
} }
} }

View file

@ -7,9 +7,8 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
public class StringSink : Sink { public class StringSink : Sink {
private readonly StringBuilder builder = new StringBuilder();
/// <summary> /// <summary>
/// The string that this sink currently contains. /// The string that this sink currently contains, constructed from the underlying <see cref="StringBuilder"/>.
/// Can be cleared using <see cref="Clear"/>. /// Can be cleared using <see cref="Clear"/>.
/// </summary> /// </summary>
public string Value { public string Value {
@ -18,6 +17,18 @@ namespace ExtremelySimpleLogger {
return this.builder.ToString(); return this.builder.ToString();
} }
} }
/// <summary>
/// The <see cref="StringBuilder"/> used to collect logged data from this sink.
/// To get its value directly, you can use <see cref="Value"/>.
/// </summary>
public StringBuilder StringBuilder {
get {
lock (this.builder)
return this.builder;
}
}
private readonly StringBuilder builder = new StringBuilder();
/// <summary> /// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>. /// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.

BIN
Logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View file

@ -1,5 +1,6 @@
# ExtremelySimpleLogger ![The ExtremelySimpleLogger logo](https://raw.githubusercontent.com/Ellpeck/ExtremelySimpleLogger/main/Logo.png)
**A very simple logger for .NET programs.**
**ExtremelySimpleLogger** is a very simple logger for .NET programs.
To set up an extremely simple logger, add a reference to the [NuGet package](https://www.nuget.org/packages/ExtremelySimpleLogger/) to your project file. Remember to change the `VERSION` to the most recent one. To set up an extremely simple logger, add a reference to the [NuGet package](https://www.nuget.org/packages/ExtremelySimpleLogger/) to your project file. Remember to change the `VERSION` to the most recent one.
```xml ```xml