mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-12-22 05:09:24 +01:00
added console colors to ConsoleSink
This commit is contained in:
parent
b989e6d541
commit
20faaf8dff
5 changed files with 38 additions and 8 deletions
|
@ -6,12 +6,36 @@ namespace ExtremelySimpleLogger {
|
|||
/// </summary>
|
||||
public class ConsoleSink : Sink {
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ConsoleColor"/> that this console sink should use when printing messages with the <see cref="LogLevel.Warn"/> log level.
|
||||
/// </summary>
|
||||
public ConsoleColor WarnColor { get; set; } = ConsoleColor.DarkYellow;
|
||||
/// <summary>
|
||||
/// The <see cref="ConsoleColor"/> that this console sink should use when printing messages with the <see cref="LogLevel.Error"/> and <see cref="LogLevel.Fatal"/> log levels.
|
||||
/// </summary>
|
||||
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
|
||||
private readonly object locker = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
||||
/// </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>
|
||||
public override void Log(string s) {
|
||||
Console.WriteLine(s);
|
||||
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||
lock (this.locker) {
|
||||
switch (level) {
|
||||
case LogLevel.Warn:
|
||||
Console.ForegroundColor = this.WarnColor;
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
case LogLevel.Fatal:
|
||||
Console.ForegroundColor = this.ErrorColor;
|
||||
break;
|
||||
}
|
||||
Console.WriteLine(s);
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,6 @@
|
|||
<PackageProjectUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/Ellpeck/ExtremelySimpleLogger</RepositoryUrl>
|
||||
<PackageLicenseUrl>https://github.com/Ellpeck/ExtremelySimpleLogger/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<VersionPrefix>1.1.0</VersionPrefix>
|
||||
<VersionPrefix>1.2.0</VersionPrefix>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
|
@ -49,8 +49,10 @@ namespace ExtremelySimpleLogger {
|
|||
/// <summary>
|
||||
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
||||
/// </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>
|
||||
public override void Log(string s) {
|
||||
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||
lock (this.file) {
|
||||
if (this.reopenOnWrite) {
|
||||
using (var w = this.file.AppendText())
|
||||
|
|
|
@ -32,14 +32,16 @@ namespace ExtremelySimpleLogger {
|
|||
/// <param name="message">The message</param>
|
||||
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
|
||||
public virtual void Log(Logger logger, LogLevel level, object message, Exception e = null) {
|
||||
this.Log(this.Formatter.Invoke(logger, level, message, e));
|
||||
this.Log(logger, level, this.Formatter.Invoke(logger, level, message, e));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message, which has already been formatted using <see cref="Formatter"/>.
|
||||
/// </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>
|
||||
public abstract void Log(string s);
|
||||
protected abstract void Log(Logger logger, LogLevel level, string s);
|
||||
|
||||
/// <summary>
|
||||
/// The default formatter for logging messages.
|
||||
|
@ -54,7 +56,7 @@ namespace ExtremelySimpleLogger {
|
|||
/// <param name="message">The message</param>
|
||||
/// <param name="e">An optional exception whose stack trace will be appended to the message</param>
|
||||
/// <returns>A formatted string to log</returns>
|
||||
public virtual string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) {
|
||||
protected virtual string FormatDefault(Logger logger, LogLevel level, object message, Exception e = null) {
|
||||
var builder = new StringBuilder();
|
||||
// date
|
||||
builder.Append($"[{DateTime.Now}] ");
|
||||
|
|
|
@ -22,8 +22,10 @@ namespace ExtremelySimpleLogger {
|
|||
/// <summary>
|
||||
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
||||
/// </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>
|
||||
public override void Log(string s) {
|
||||
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||
lock (this.builder)
|
||||
this.builder.AppendLine(s);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue