/// <summary> /// Adds a log writer and binds to a specific log message type /// </summary> /// <param name="logMessageType">Type of log message</param> /// <param name="logWriter">Log writer to add</param> public void AddLogWriter(NiawaLogType logMessageType, ILogWriter logWriter) { //Ensure that more than one instance of a ILogWriter implementation (of a specific type) is not added. if(_logWriters.ContainsKey(logWriter.GetType().ToString())) if (_logWriters[logWriter.GetType().ToString()].LogWriterID != logWriter.LogWriterID) throw new Exception("Cannot add two instances of the same ILogWriter type [" + logWriter.GetType().ToString() + "]. Add a different type of ILogWriter, or add an existing instance of this ILogWriter type."); //Check for duplicate entry attempts into the LogMessageTypeLogWriters collection if (_logMessageTypeLogWriters.ContainsKey(logMessageType)) if (_logMessageTypeLogWriters[logMessageType].Contains(logWriter)) throw new Exception("Cannot add the same ILogWriter [" + logWriter.GetType().ToString() + "] for LogMessageType [" + Enum.GetName(logMessageType.GetType(), logMessageType) + "] more than once."); //add log writer to list if (!(_logWriters.ContainsKey(logWriter.GetType().ToString()))) _logWriters.Add(logWriter.GetType().ToString(), logWriter); //add log writer to map List<ILogWriter> mapLogWriters = null; //check if log message type already in map if (_logMessageTypeLogWriters.ContainsKey(logMessageType)) mapLogWriters = _logMessageTypeLogWriters[logMessageType]; else { //create new entry of log writers for log message type and add to map mapLogWriters = new List<ILogWriter>(); _logMessageTypeLogWriters.Add(logMessageType, mapLogWriters); } //add log writer to list of log writers mapped to log message type mapLogWriters.Add(logWriter); }
private static void RemoveWriter(ILogWriter writer) { if (writer.UseBackgroundThreadQueue) { BackgroundThreadQueue.Remove(writer); } else { DirectWriters.Remove(writer); } // adding a message as soon as the logger is removed so that the logger has a timestamp in its log for when it was removed WriteTrace($"LogWriter '{writer.GetType().Name}'({(writer.UseBackgroundThreadQueue ? "queued" : "not queued")}) removed", typeof(Log).FullName, MethodBase.GetCurrentMethod().Name); }
static void Main(string[] args) { string warningText = "warning by"; string errorText = "error by"; string infoText = "info by"; string stringSeparator = " "; LogWriterFactory factory = LogWriterFactory.GetLogWriterFactoryInstance(); ILogWriter consoleLogWriter = factory.GetLogWriter <ConsoleLogWriter>(null); consoleLogWriter.LogWarning(warningText + stringSeparator + consoleLogWriter.GetType()); consoleLogWriter.LogError(errorText + stringSeparator + consoleLogWriter.GetType()); consoleLogWriter.LogInfo(infoText + stringSeparator + consoleLogWriter.GetType()); ILogWriter fileLogWriter = factory.GetLogWriter <FileLogWriter>($"log.txt"); fileLogWriter.LogWarning(warningText + stringSeparator + fileLogWriter.GetType()); fileLogWriter.LogError(errorText + stringSeparator + fileLogWriter.GetType()); fileLogWriter.LogInfo(infoText + stringSeparator + fileLogWriter.GetType()); ILogWriter oneMoreConsoleWriter = new ConsoleLogWriter(); ILogWriter oneMoreFileWriter = new FileLogWriter($"log.txt"); List <ILogWriter> LogWriterList = new List <ILogWriter> { oneMoreConsoleWriter, oneMoreFileWriter }; ILogWriter multipleLogWriter = factory.GetLogWriter <MultipleLogWriter>(LogWriterList); multipleLogWriter.LogWarning(warningText + stringSeparator + multipleLogWriter.GetType()); multipleLogWriter.LogError(errorText + stringSeparator + multipleLogWriter.GetType()); multipleLogWriter.LogInfo(infoText + stringSeparator + multipleLogWriter.GetType()); }
public static void RemoveWriter(ILogWriter logWriter, string?reason = null, LogLevel logLevel = LogLevel.Info) { if (LogWriters.Contains(logWriter)) { LogWriters = LogWriters.Where(w => w != logWriter).ToArray(); if (!string.IsNullOrEmpty(reason)) { string message = $"Removed {logWriter.GetType().Name}: {reason}"; if (IsAlive && HasWriters) { QueueMessage(message, logLevel); } else { Console.WriteLine(message); } } } }
public override bool Equals(ILogWriter other) { return(other != null && typeof(ConsoleLogWriter).IsAssignableFrom(other.GetType())); }
internal MockLog(ILogWriter logWriter) : this(logWriter.GetType()) { logWriter.Log = new ProxyLog(this); }