Skip to content

Instantly share code, notes, and snippets.

@paddybyers
Created August 26, 2016 21:56
Show Gist options
  • Save paddybyers/44dc0192a750a378f0fb99d344aba80f to your computer and use it in GitHub Desktop.
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();`
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