Compare commits

..

No commits in common. "2e3ebe9a528a57163f76188e651d332bcdf91edd" and "7a5929f8e8709057b24325a3334ab60a478f33bb" have entirely different histories.

6 changed files with 26 additions and 78 deletions

View file

@ -36,14 +36,9 @@ namespace ExtremelySimpleLogger {
public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) {
this.reopenOnWrite = reopenOnWrite;
try {
if (!directory.Exists)
directory.Create();
} catch (Exception e) {
throw new IOException($"Failed to create directory sink directory {directory}", e);
}
try {
// delete old files
var files = directory.GetFiles();
if (files.Length >= maxFiles) {
@ -54,14 +49,11 @@ namespace ExtremelySimpleLogger {
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.Append();
this.writer = this.file.AppendText();
this.writer.AutoFlush = true;
}
}
@ -75,7 +67,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) {
if (this.reopenOnWrite) {
using (var w = this.Append())
using (var w = this.file.AppendText())
w.WriteLine(s);
} else {
this.writer.WriteLine(s);
@ -94,13 +86,5 @@ 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,13 +12,6 @@
<PackageProjectUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.2.4</VersionPrefix>
<VersionPrefix>1.2.3</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
<None Include="../Logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View file

@ -1,4 +1,3 @@
using System;
using System.IO;
namespace ExtremelySimpleLogger {
@ -34,23 +33,15 @@ namespace ExtremelySimpleLogger {
this.reopenOnWrite = reopenOnWrite;
this.file = file;
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);
}
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 = this.Append();
this.writer = file.AppendText();
this.writer.AutoFlush = true;
}
}
@ -64,7 +55,7 @@ namespace ExtremelySimpleLogger {
protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.file) {
if (this.reopenOnWrite) {
using (var w = this.Append())
using (var w = this.file.AppendText())
w.WriteLine(s);
} else {
this.writer.WriteLine(s);
@ -83,13 +74,5 @@ 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,8 +7,9 @@ namespace ExtremelySimpleLogger {
/// </summary>
public class StringSink : Sink {
private readonly StringBuilder builder = new StringBuilder();
/// <summary>
/// The string that this sink currently contains, constructed from the underlying <see cref="StringBuilder"/>.
/// The string that this sink currently contains.
/// Can be cleared using <see cref="Clear"/>.
/// </summary>
public string Value {
@ -17,18 +18,6 @@ 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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

View file

@ -1,6 +1,5 @@
![The ExtremelySimpleLogger logo](https://raw.githubusercontent.com/Ellpeck/ExtremelySimpleLogger/main/Logo.png)
**ExtremelySimpleLogger** is a very simple logger for .NET programs.
# ExtremelySimpleLogger
**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