Created
August 26, 2016 21:56
-
-
Save paddybyers/44dc0192a750a378f0fb99d344aba80f to your computer and use it in GitHub Desktop.
Console logger for Ably .NET library; set up with `Logger.LoggerSink = new MyLogger();`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace IO.Ably.ConsoleTest | |
{ | |
/// <summary>ILoggerSink implementation that outputs colored messages to console.</summary> | |
class MyLogger : ILoggerSink | |
{ | |
static readonly Dictionary<LogLevel, ConsoleColor> s_colors = new Dictionary<LogLevel, ConsoleColor>() | |
{ | |
{ LogLevel.Error, ConsoleColor.Red }, | |
{ LogLevel.Warning, ConsoleColor.Yellow}, | |
{ LogLevel.Debug, ConsoleColor.Cyan }, | |
}; | |
void ILoggerSink.LogEvent(LogLevel level, string message) | |
{ | |
ConsoleEx.WriteLine(s_colors[level], " " + message); | |
} | |
} | |
static class ConsoleEx | |
{ | |
static readonly object syncRoot = new object(); | |
public static void WriteLine(this ConsoleColor cc, string message) | |
{ | |
if (silent) | |
return; | |
lock (syncRoot) | |
{ | |
ConsoleColor oc = Console.ForegroundColor; | |
Console.ForegroundColor = cc; | |
Console.WriteLine(message); | |
Console.ForegroundColor = oc; | |
} | |
} | |
public static void WriteLine(this ConsoleColor cc, string message, params object[] args) | |
{ | |
if (silent) | |
return; | |
lock (syncRoot) | |
{ | |
ConsoleColor oc = Console.ForegroundColor; | |
Console.ForegroundColor = cc; | |
Console.WriteLine(message, args); | |
Console.ForegroundColor = oc; | |
} | |
} | |
public static void Write(this ConsoleColor cc, string message) | |
{ | |
if (silent) | |
return; | |
lock (syncRoot) | |
{ | |
ConsoleColor oc = Console.ForegroundColor; | |
Console.ForegroundColor = cc; | |
Console.Write(message); | |
Console.ForegroundColor = oc; | |
} | |
} | |
public static void LogError(this Exception ex) | |
{ | |
if (ex is AggregateException) | |
ex = (ex as AggregateException).Flatten().InnerExceptions.First(); | |
lock (syncRoot) | |
{ | |
WriteLine(ConsoleColor.Red, ex.Message); | |
WriteLine(ConsoleColor.DarkRed, ex.ToString()); | |
} | |
} | |
public static bool silent = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment