mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-24 18:48:34 +01:00
Compare commits
5 commits
7a5929f8e8
...
2e3ebe9a52
Author | SHA1 | Date | |
---|---|---|---|
2e3ebe9a52 | |||
35807eb330 | |||
7b8084a8a7 | |||
5ab6fd855e | |||
46bb08f60b |
6 changed files with 78 additions and 26 deletions
|
@ -36,9 +36,14 @@ 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) {
|
||||
|
@ -49,11 +54,14 @@ 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.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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ExtremelySimpleLogger {
|
||||
|
@ -33,15 +34,23 @@ 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 = 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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
BIN
Logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue