mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-22 01:53:29 +01:00
allow writing to Console.Error using ConsoleSink
This commit is contained in:
parent
b9b6872949
commit
fff9f16068
1 changed files with 13 additions and 3 deletions
|
@ -1,9 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace ExtremelySimpleLogger {
|
||||
/// <summary>
|
||||
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/>.
|
||||
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/> or <see cref="Console.Error"/>.
|
||||
/// </summary>
|
||||
public class ConsoleSink : Sink {
|
||||
|
||||
|
@ -17,6 +18,15 @@ namespace ExtremelySimpleLogger {
|
|||
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
||||
};
|
||||
private readonly object locker = new object();
|
||||
private readonly TextWriter console;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new console sink with the given settings.
|
||||
/// </summary>
|
||||
/// <param name="error">Whether to log to <see cref="Console.Error"/> instead of <see cref="Console.Out"/>.</param>
|
||||
public ConsoleSink(bool error = false) {
|
||||
this.console = error ? Console.Error : Console.Out;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> should be displayed with.
|
||||
|
@ -57,11 +67,11 @@ namespace ExtremelySimpleLogger {
|
|||
var color = this.GetColor(level);
|
||||
if (color.HasValue)
|
||||
Console.ForegroundColor = color.Value;
|
||||
Console.WriteLine(s);
|
||||
this.console.WriteLine(s);
|
||||
if (color.HasValue)
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue