added LogWriter class

This commit is contained in:
Ell 2023-11-30 22:46:16 +01:00
parent 56b0240d71
commit d39abe603d
2 changed files with 89 additions and 1 deletions

View file

@ -0,0 +1,82 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ExtremelySimpleLogger {
/// <summary>
/// Implementation of a <see cref="TextWriter"/> that writes to a <see cref="Logger"/>.
/// </summary>
public class LogWriter : TextWriter {
/// <summary>
/// The log level that this log writer should write with.
/// </summary>
public LogLevel Level {
get {
lock (this.logger)
return this.level;
}
set {
lock (this.logger)
this.level = value;
}
}
/// <inheritdoc />
public override Encoding Encoding => Encoding.UTF8;
private readonly StringBuilder line = new StringBuilder();
private readonly Logger logger;
private LogLevel level;
/// <summary>
/// Creates a new log writer with the given settings.
/// </summary>
/// <param name="logger">The logger to write to.</param>
/// <param name="level">The log level to write with.</param>
public LogWriter(Logger logger, LogLevel level = LogLevel.Info) {
this.logger = logger;
this.level = level;
}
/// <inheritdoc />
public override void Write(char value) {
lock (this.logger)
this.line.Append(value);
}
/// <inheritdoc />
public override void Write(char[] buffer, int index, int count) {
lock (this.logger)
this.line.Append(buffer, index, count);
}
/// <inheritdoc />
public override void Write(string value) {
lock (this.logger)
this.line.Append(value);
}
/// <inheritdoc />
public override void WriteLine(string value) {
this.Write(value);
this.WriteLine();
}
/// <inheritdoc />
public override void WriteLine() {
this.Flush();
}
/// <inheritdoc />
public override void Flush() {
lock (this.logger) {
this.logger.Log(this.level, this.line);
this.line.Clear();
}
}
}
}

View file

@ -39,9 +39,15 @@ namespace Sample {
logger.Log(LogLevel.Trace, "This is a message that only the file sink will receive, since its minimum level is lower.");
logger.Log(LogLevel.Info, "The program finished.");
// we can also use a writer to write to the log
Console.SetError(new LogWriter(logger, LogLevel.Warn));
Console.Error.WriteLine("This is an error written through serr! Oh no!");
Console.Error.Write("This is another error, but ");
Console.Error.WriteLine("written in multiple parts!");
// Once we're done using the logger, we can dispose it so that our FileSink instances free their files
logger.Dispose();
}
}
}
}