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) {
this.reopenOnWrite = reopenOnWrite;
if (!directory.Exists)
directory.Create();
try {
if (!directory.Exists)
directory.Create();
} catch (Exception e) {
throw new IOException($"Failed to create directory sink directory {directory}", e);
}
// 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);
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);
}
}
} catch (Exception e) {
throw new IOException($"Failed to delete old files in directory sink {directory}", e);
}
var date = DateTime.Now.ToString(dateFormat);
this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt"));
if (!reopenOnWrite) {
this.writer = this.file.AppendText();
this.writer = this.Append();
this.writer.AutoFlush = true;
}
}
@ -67,7 +75,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) {
if (this.reopenOnWrite) {
using (var w = this.file.AppendText())
using (var w = this.Append())
w.WriteLine(s);
} else {
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>
<RepositoryUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<VersionPrefix>1.2.3</VersionPrefix>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.2.4</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
<None Include="../Logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View file

@ -1,3 +1,4 @@
using System;
using System.IO;
namespace ExtremelySimpleLogger {
@ -33,15 +34,23 @@ namespace ExtremelySimpleLogger {
this.reopenOnWrite = reopenOnWrite;
this.file = file;
var dir = file.Directory;
if (dir != null && !dir.Exists)
dir.Create();
try {
var dir = file.Directory;
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))
file.Delete();
try {
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) {
this.writer = file.AppendText();
this.writer = this.Append();
this.writer.AutoFlush = true;
}
}
@ -55,7 +64,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) {
if (this.reopenOnWrite) {
using (var w = this.file.AppendText())
using (var w = this.Append())
w.WriteLine(s);
} else {
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>
public class StringSink : Sink {
private readonly StringBuilder builder = new StringBuilder();
/// <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"/>.
/// </summary>
public string Value {
@ -18,6 +17,18 @@ namespace ExtremelySimpleLogger {
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>
/// 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
**A very simple logger for .NET programs.**
![The ExtremelySimpleLogger logo](https://raw.githubusercontent.com/Ellpeck/ExtremelySimpleLogger/main/Logo.png)
**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.
```xml