/// <summary> /// Main method in the ILogger interface that defines how to print a message. /// </summary> /// <param name="logLevel">Log severity</param> /// <param name="eventId">Event category (no used in this project)</param> /// <param name="state">Actual message wrapped</param> /// <param name="exception">Associated exception</param> /// <param name="formatter">Function that defines how to format a message.</param> public void Log <TState>( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter ) { /// <summary> /// Return immediately is logging is not enabled for this log level /// </summary> if (!IsEnabled(logLevel)) { return; } // Exception has to be set (by framework) if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } // Generate actual message var message = formatter(state, exception); // Return for empty message if (string.IsNullOrEmpty(message)) { return; } if (logLevel >= LogLevel.Error) { _loggingService.RecordLogMessageAsync( message, exception != null ? JsonConvert.SerializeObject(new { Exception = exception.ToString() }) : "", "status-site", eventId.Id, logLevel.GetSeverity() ); } // Define colors for thread identifiers var threadColors = new ConsoleColor[] { ConsoleColor.Cyan, ConsoleColor.DarkCyan, ConsoleColor.Blue, ConsoleColor.DarkBlue, ConsoleColor.DarkGreen, ConsoleColor.DarkMagenta, ConsoleColor.DarkYellow, ConsoleColor.DarkRed }; // Generate thread label text var thread = Thread.CurrentThread; var threadLabel = string.IsNullOrEmpty(thread.Name) ? $"ThreadID: {thread.ManagedThreadId.ToString().PadLeft(3)}" : thread.Name.Substring(0, 12); // Generate a thread label color var threadColor = threadColors[Math.Abs(threadLabel.GetHashCode()) % threadColors.Length]; var label = logLevel.GetLabel(); // Do not let multiple threads enter the actual print section lock (StatusLogger._lockKey) { // Print everything (watch for pads and new lines) ColoredConsole.Write($"{threadLabel}", threadColor); Console.Write("".PadLeft(6 - label.Text.Length)); ColoredConsole.Write($"{label.Text}", label.ForegroundColor, label.BackgroundColor); Console.Write(" | "); Console.Write($"[{eventId.ToString().PadRight(2)}] {_categoryName}"); Console.WriteLine(); Console.Write($"[{DateTime.UtcNow.ToString("MM/dd/yy HH:mm:ss")}]"); Console.Write(" | "); if (exception != null) { message += Environment.NewLine + Environment.NewLine + exception.ToString(); } if (message.Contains(Environment.NewLine)) { message = "See multiline message:" + Environment.NewLine + "\t" + message.Replace(Environment.NewLine, $"{Environment.NewLine}\t"); } Console.WriteLine(message); Console.WriteLine(); } }