Exemple #1
0
        private static void ConsoleLogWrite(LogEntry logEntry, bool force = false)
        {
            if (ConsoleWriting && !force) {
                ConsoleWaitQueue.Enqueue(logEntry);
                return;
            }

            ConsoleWriting = true;
            Console.Write(($"<{logEntry.Instance}> ").PadLeft(56));

            switch (logEntry.Level)
            {
                case LogSeverity.Severe:
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case LogSeverity.Warning:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
            }

            Console.Write(logEntry.Line);
            Console.ResetColor();
            Console.WriteLine();

            if (ConsoleWaitQueue.Count > 0) {
                ConsoleLogWrite(ConsoleWaitQueue.Dequeue(), true);
            } else {
                ConsoleWriting = false;
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes to the log.
        /// </summary>
        /// <param name="level">Severity of the log.</param>
        /// <param name="text">Actual (formattable) text.</param>
        /// <param name="args">string.Format arguments.</param>
        public void Write(LogSeverity level, string text, params string[] args)
        {
            if (args.Length > 0) {
                text = string.Format(text, args);
            }

            string[] split = text.Split('\n');

            if (split.Length > 1) {
                foreach (string part in split) {
                    Write(level, part);
                }
                return;
            }

            string line = string.Format(Format, new string[3] {
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                level.ToString().PadLeft(8),
                text
            });

            LogFile.WriteLine(line);

            LogEntry logEntry = new LogEntry {
                Text = text,
                Instance = InstanceName,
                Level = level,
                Line = line
            };

            (new Thread(() => {
                foreach (Action<LogEntry> action in LogWrite) {
                    action.Invoke(logEntry);
                }
            })).Start();
        }