added WriterSink

This commit is contained in:
Ell 2023-12-01 10:54:00 +01:00
parent 096bc98a05
commit cb824c73fc
2 changed files with 45 additions and 2 deletions

View file

@ -5,6 +5,7 @@ using System.IO;
namespace ExtremelySimpleLogger { namespace ExtremelySimpleLogger {
/// <summary> /// <summary>
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/> or <see cref="Console.Error"/>. /// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/> or <see cref="Console.Error"/>.
/// This class is a variation of the <see cref="WriterSink"/>.
/// </summary> /// </summary>
public class ConsoleSink : Sink { public class ConsoleSink : Sink {
@ -17,7 +18,6 @@ namespace ExtremelySimpleLogger {
{LogLevel.Error, ConsoleColor.DarkRed}, {LogLevel.Error, ConsoleColor.DarkRed},
{LogLevel.Fatal, ConsoleColor.DarkRed} {LogLevel.Fatal, ConsoleColor.DarkRed}
}; };
private readonly object locker = new object();
private readonly TextWriter console; private readonly TextWriter console;
/// <summary> /// <summary>
@ -63,7 +63,7 @@ namespace ExtremelySimpleLogger {
/// <param name="level">The importance level of this message</param> /// <param name="level">The importance level of this message</param>
/// <param name="s">The message to log</param> /// <param name="s">The message to log</param>
protected override void Log(Logger logger, LogLevel level, string s) { protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.locker) { lock (this.console) {
var color = this.GetColor(level); var color = this.GetColor(level);
if (color.HasValue) if (color.HasValue)
Console.ForegroundColor = color.Value; Console.ForegroundColor = color.Value;

View file

@ -0,0 +1,43 @@
using System.IO;
namespace ExtremelySimpleLogger {
/// <summary>
/// A <see cref="Sink"/> that writes log output to an underlying <see cref="TextWriter"/>.
/// Note that <see cref="ConsoleSink"/> is a variation of this sink that additionally includes console colors.
/// </summary>
public class WriterSink : Sink {
private readonly TextWriter writer;
private readonly bool autoClose;
/// <summary>
/// Creates a new writer sink with the given settings.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="autoClose">Whether the underlying <paramref name="writer"/> should be closed automatically when this sink is disposed in <see cref="Dispose"/>.</param>
public WriterSink(TextWriter writer, bool autoClose = false) {
this.writer = writer;
this.autoClose = autoClose;
}
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
/// </summary>
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
/// <param name="s">The message to log</param>
protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.writer)
this.writer.WriteLine(s);
}
/// <inheritdoc />
public override void Dispose() {
if (this.autoClose) {
lock (this.writer)
this.writer.Dispose();
}
}
}
}