mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-25 02:58:34 +01:00
Compare commits
No commits in common. "2e3ebe9a528a57163f76188e651d332bcdf91edd" and "7a5929f8e8709057b24325a3334ab60a478f33bb" have entirely different histories.
2e3ebe9a52
...
7a5929f8e8
6 changed files with 26 additions and 78 deletions
|
@ -36,32 +36,24 @@ 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;
|
||||||
|
|
||||||
try {
|
if (!directory.Exists)
|
||||||
if (!directory.Exists)
|
directory.Create();
|
||||||
directory.Create();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException($"Failed to create directory sink directory {directory}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// delete old files
|
||||||
// delete old files
|
var files = directory.GetFiles();
|
||||||
var files = directory.GetFiles();
|
if (files.Length >= maxFiles) {
|
||||||
if (files.Length >= maxFiles) {
|
// order files by their creation time so that older files are deleted first
|
||||||
// order files by their creation time so that older files are deleted first
|
var ordered = files.OrderBy(f => f.CreationTime).ToList();
|
||||||
var ordered = files.OrderBy(f => f.CreationTime).ToList();
|
while (ordered.Count >= maxFiles) {
|
||||||
while (ordered.Count >= maxFiles) {
|
ordered[0].Delete();
|
||||||
ordered[0].Delete();
|
ordered.RemoveAt(0);
|
||||||
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.Append();
|
this.writer = this.file.AppendText();
|
||||||
this.writer.AutoFlush = true;
|
this.writer.AutoFlush = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +67,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.Append())
|
using (var w = this.file.AppendText())
|
||||||
w.WriteLine(s);
|
w.WriteLine(s);
|
||||||
} else {
|
} else {
|
||||||
this.writer.WriteLine(s);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,13 +12,6 @@
|
||||||
<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>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<VersionPrefix>1.2.3</VersionPrefix>
|
||||||
<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>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace ExtremelySimpleLogger {
|
namespace ExtremelySimpleLogger {
|
||||||
|
@ -34,23 +33,15 @@ namespace ExtremelySimpleLogger {
|
||||||
this.reopenOnWrite = reopenOnWrite;
|
this.reopenOnWrite = reopenOnWrite;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
|
|
||||||
try {
|
var dir = file.Directory;
|
||||||
var dir = file.Directory;
|
if (dir != null && !dir.Exists)
|
||||||
if (dir != null && !dir.Exists)
|
dir.Create();
|
||||||
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))
|
||||||
if (file.Exists && (!append || file.Length >= fileSizeLimit))
|
file.Delete();
|
||||||
file.Delete();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException($"Failed to delete file sink file {file}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!reopenOnWrite) {
|
if (!reopenOnWrite) {
|
||||||
this.writer = this.Append();
|
this.writer = file.AppendText();
|
||||||
this.writer.AutoFlush = true;
|
this.writer.AutoFlush = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +55,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.Append())
|
using (var w = this.file.AppendText())
|
||||||
w.WriteLine(s);
|
w.WriteLine(s);
|
||||||
} else {
|
} else {
|
||||||
this.writer.WriteLine(s);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,8 +7,9 @@ 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, constructed from the underlying <see cref="StringBuilder"/>.
|
/// The string that this sink currently contains.
|
||||||
/// Can be cleared using <see cref="Clear"/>.
|
/// Can be cleared using <see cref="Clear"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Value {
|
public string Value {
|
||||||
|
@ -17,18 +18,6 @@ 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
BIN
Logo.png
Binary file not shown.
Before Width: | Height: | Size: 13 KiB |
|
@ -1,6 +1,5 @@
|
||||||
![The ExtremelySimpleLogger logo](https://raw.githubusercontent.com/Ellpeck/ExtremelySimpleLogger/main/Logo.png)
|
# ExtremelySimpleLogger
|
||||||
|
**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
|
||||||
|
|
Loading…
Reference in a new issue