Added StringSink, which writes data to a string

This commit is contained in:
Ellpeck 2020-07-23 13:56:01 +02:00
parent a518c5c674
commit b3878887e3
3 changed files with 37 additions and 2 deletions

View file

@ -6,13 +6,13 @@ namespace ExtremelySimpleLogger {
/// The implementation of a logger, which is a wrapper class around multiple logging <see cref="Sinks"/>. /// The implementation of a logger, which is a wrapper class around multiple logging <see cref="Sinks"/>.
/// ///
/// To start logging with a logger, its <see cref="Sinks"/> need to be initialized. /// To start logging with a logger, its <see cref="Sinks"/> need to be initialized.
/// There are two default implementations: <see cref="FileSink"/> and <see cref="ConsoleSink"/>. /// There are two default implementations: <see cref="FileSink"/>, <see cref="ConsoleSink"/> and <see cref="StringSink"/>.
/// </summary> /// </summary>
public class Logger { public class Logger {
/// <summary> /// <summary>
/// All of the <see cref="Sink"/> instances that this logger logs to. /// All of the <see cref="Sink"/> instances that this logger logs to.
/// By default, <see cref="FileSink"/> and <see cref="ConsoleSink"/> are available. /// By default, <see cref="FileSink"/>, <see cref="ConsoleSink"/> and <see cref="StringSink"/> are available.
/// </summary> /// </summary>
public List<Sink> Sinks { get; set; } = new List<Sink>(); public List<Sink> Sinks { get; set; } = new List<Sink>();
/// <summary> /// <summary>

View file

@ -0,0 +1,34 @@
using System.Text;
namespace ExtremelySimpleLogger {
/// <summary>
/// A <see cref="Sink"/> that writes output to a string, which can be queried using <see cref="Value"/>.
/// Note that this uses a <see cref="StringBuilder"/> internally for performance.
/// </summary>
public class StringSink : Sink {
private readonly StringBuilder builder = new StringBuilder();
/// <summary>
/// The string that this sink currently contains.
/// Can be cleared using <see cref="Clear"/>.
/// </summary>
public string Value => this.builder.ToString();
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
/// <param name="s">The message to log</param>
public override void Log(string s) {
this.builder.AppendLine(s);
}
/// <summary>
/// Clears the string that this sink currently contains.
/// After this call, <see cref="Value"/> will be empty.
/// </summary>
public void Clear() {
this.builder.Clear();
}
}
}

View file

@ -22,6 +22,7 @@ var logger = new Logger {
Since there are multiple ways for logging data to be processed, the logger needs to receive a set of `Sink` instances. By default, the following sinks are available: Since there are multiple ways for logging data to be processed, the logger needs to receive a set of `Sink` instances. By default, the following sinks are available:
- `FileSink`, which outputs logging data to a file - `FileSink`, which outputs logging data to a file
- `ConsoleSink`, which outputs logging data to the default console - `ConsoleSink`, which outputs logging data to the default console
- `StringSink`, which stores logging data in a string
There are multiple ways to easily log messages with your newly created logger: There are multiple ways to easily log messages with your newly created logger:
```cs ```cs