public void Log(LogLevel level, string timestamp, Type source, string category, string message) { level.CheckValidity(nameof(level)); if (source == null) { throw new ArgumentNullException(nameof(source)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } ConsoleColor color; string lvl; switch (level) { case LogLevel.Error: color = _errorColor; lvl = "E"; break; case LogLevel.Warning: color = _warningColor; lvl = "W"; break; case LogLevel.Basic: color = _basicColor; lvl = "B"; break; case LogLevel.Info: color = _infoColor; lvl = "I"; break; case LogLevel.Debug: color = _debugColor; lvl = "D"; break; default: throw Assert.Unreachable(); } timestamp = timestamp != null ? $"[{timestamp}] " : string.Empty; category = category != null ? $" ({category})" : string.Empty; var console = level <= LogLevel.Warning ? Console.Error : Console.Out; Console.ForegroundColor = color; console.WriteLine($"{timestamp}[{lvl}] {source.Name}{category}: {message}"); Console.ResetColor(); }
public void Log(LogLevel level, string timestamp, Type source, string category, string message) { level.CheckValidity(nameof(level)); if (source == null) { throw new ArgumentNullException(nameof(source)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } string lvl; switch (level) { case LogLevel.Error: lvl = "E"; break; case LogLevel.Warning: lvl = "W"; break; case LogLevel.Basic: lvl = "B"; break; case LogLevel.Info: lvl = "I"; break; case LogLevel.Debug: lvl = "D"; break; default: throw Assert.Unreachable(); } timestamp = timestamp != null ? $"[{timestamp}] " : string.Empty; category = category != null ? $" ({category})" : string.Empty; _writer.WriteLine($"{timestamp}[{lvl}] {source.Name}{category}: {message}"); _writer.Flush(); }
public void Log(LogLevel level, string timestamp, Type source, string category, string message) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } string lvl; switch (level.CheckValidity(nameof(level))) { case LogLevel.Error: lvl = "E"; break; case LogLevel.Warning: lvl = "W"; break; case LogLevel.Basic: lvl = "B"; break; case LogLevel.Info: lvl = "I"; break; case LogLevel.Debug: lvl = "D"; break; default: throw Assert.Unreachable(); } var sb = new StringBuilder(); if (_timestamp && timestamp != null) { sb.AppendFormat("[{0}] ", timestamp); } if (_level) { sb.AppendFormat("[{0}] ", lvl); } if (_source) { sb.AppendFormat("{0}", source.Name); if (category != null) { sb.AppendFormat(" ({0})", category); } sb.Append(": "); } sb.Append(message); _writer.WriteLine(sb.ToString()); _writer.Flush(); }