// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using log4net; using log4net.Config; using log4net.Appender; using log4net.Core; namespace Blue.One { public class CB1 { private ILog log = LogManager.GetLogger(typeof(CB1).FullName); public void Test() { log.Fatal("Just testing"); log.Error("Just testing"); log.Warn("Just testing"); log.Info("Just testing"); log.Debug("Just testing"); } } } namespace Blue.Two { public class CB2 { private ILog log = LogManager.GetLogger(typeof(CB2).FullName); public void Test() { log.Fatal("Just testing"); log.Error("Just testing"); log.Warn("Just testing"); log.Info("Just testing"); log.Debug("Just testing"); } } } namespace Red.One { public class CR1 { private ILog log = LogManager.GetLogger(typeof(CR1).FullName); public void Test() { log.Fatal("Just testing"); log.Error("Just testing"); log.Warn("Just testing"); log.Info("Just testing"); log.Debug("Just testing"); } } } namespace Red.Two { public class CR2 { private ILog log = LogManager.GetLogger(typeof(CR2).FullName); public void Test() { log.Fatal("Just testing"); log.Error("Just testing"); log.Warn("Just testing"); log.Info("Just testing"); log.Debug("Just testing"); } } } namespace E { public class AdvancedColoredConsoleAppender : AppenderSkeleton { public string Map { get; set; } private Dictionary, ConsoleColor[]> colmap = new Dictionary, ConsoleColor[]>(); private static Level ParseLevel(string s) { switch(s) { case "Fatal": return Level.Fatal; case "Error": return Level.Error; case "Warn": return Level.Warn; case "Info": return Level.Info; case "Debug": return Level.Debug; default: throw new Exception("Unknown level: " + s); } } public override void ActivateOptions() { XmlDocument doc = new XmlDocument(); doc.LoadXml(Map); foreach(XmlElement logger in doc.SelectNodes("/map/logger")) { string pref = logger.GetAttribute("prefix"); foreach(XmlElement color in logger.SelectNodes("color")) { Level lvl = ParseLevel(color.GetAttribute("level")); ConsoleColor[] cols = color.FirstChild.Value.Split('/').Select(s => (ConsoleColor)Enum.Parse(typeof(ConsoleColor), s)).ToArray(); colmap.Add(Tuple.Create(pref, lvl), cols); } } } private Level[] alllevels = { Level.Fatal, Level.Error, Level.Warn, Level.Info, Level.Debug }; protected override void Append(LoggingEvent evt) { using(StringWriter sw = new StringWriter()) { Layout.Format(sw, evt); lock(this) { ConsoleColor savfore = Console.ForegroundColor; ConsoleColor savback = Console.BackgroundColor; for(int i = 1; i < evt.LoggerName.Length; i++) { string pref = evt.LoggerName.Substring(0, i); foreach(Level lvl in alllevels) { if(evt.Level >= lvl && colmap.ContainsKey(Tuple.Create(pref, lvl))) { ConsoleColor[] cols = colmap[Tuple.Create(pref, lvl)]; Console.ForegroundColor = cols[0]; Console.BackgroundColor = cols[1]; break; } } } Console.Write(sw.ToString()); Console.ForegroundColor = savfore; Console.BackgroundColor = savback; } } } } public class Program { public static void Main(string[] args) { XmlConfigurator.Configure(); (new Blue.One.CB1()).Test(); (new Blue.Two.CB2()).Test(); (new Red.One.CR1()).Test(); (new Red.Two.CR2()).Test(); } } }