mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-12-22 05:09:24 +01:00
added LogWriter class
This commit is contained in:
parent
56b0240d71
commit
d39abe603d
2 changed files with 89 additions and 1 deletions
82
ExtremelySimpleLogger/LogWriter.cs
Normal file
82
ExtremelySimpleLogger/LogWriter.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue