/// <summary> /// Gets the color of the output of the message (the output message has a new line char in the end). /// </summary> /// <param name="logEvent">The <see cref="LogMessageReceivedEventArgs" /> instance containing the event data.</param> /// <returns> /// The output message formatted and the color of the console to be used. /// </returns> protected (string outputMessage, ConsoleColor color) GetOutputAndColor(LogMessageReceivedEventArgs logEvent) { var(prefix, color) = GetConsoleColorAndPrefix(logEvent.MessageType); var loggerMessage = string.IsNullOrWhiteSpace(logEvent.Message) ? string.Empty : logEvent.Message.RemoveControlChars('\n'); var outputMessage = CreateOutputMessage(logEvent.Source, loggerMessage, prefix, logEvent.UtcDate); // Further format the output in the case there is an exception being logged if (logEvent.MessageType == LogLevel.Error && logEvent.Exception != null) { try { outputMessage += $"{logEvent.Exception.Stringify().Indent()}{Environment.NewLine}"; } catch { // Ignore } } return(outputMessage, color); }
internal static ConsoleColor GetOutputAndColor( LogMessageReceivedEventArgs logEvent, bool isError, out string outputMessage) { var prefix = GetConsoleColorAndPrefix(logEvent.MessageType, out var color); var loggerMessage = string.IsNullOrWhiteSpace(logEvent.Message) ? string.Empty : logEvent.Message.RemoveControlCharsExcept('\n'); outputMessage = CreateOutputMessage(logEvent.Source, loggerMessage, prefix, logEvent.UtcDate); // Further format the output in the case there is an exception being logged if (isError && logEvent.Exception != null) { try { outputMessage = $"{outputMessage}{Environment.NewLine}{logEvent.Exception.Stringify().Indent()}"; } catch { // Ignore } } return(color); }
/// <inheritdoc /> public void Log(LogMessageReceivedEventArgs logEvent) { // Select the writer based on the message type var writer = logEvent.MessageType == LogLevel.Error ? TerminalWriters.StandardError : TerminalWriters.StandardOutput; var(outputMessage, color) = GetOutputAndColor(logEvent); Terminal.Write(outputMessage, color, writer); }
/// <inheritdoc /> public void Log([NotNull] LogMessageReceivedEventArgs logEvent) { lock (SyncLock) { var isError = logEvent.MessageType.HasFlag(LogLevel.Error); // Select the writer based on the message type var writer = isError ? TerminalWriters.StandardError : TerminalWriters.StandardOutput; var color = GetOutputAndColor(logEvent, isError, out var outputMessage); Terminal.WriteLine(outputMessage, color, writer); } }
private static void LogMessage( LogLevel logLevel, string message, string?sourceName, object?extendedData, string callerMemberName, string callerFilePath, int callerLineNumber) { var sequence = _loggingSequence; var date = DateTime.UtcNow; _loggingSequence++; var loggerMessage = string.IsNullOrWhiteSpace(message) ? string.Empty : message.RemoveControlChars('\n'); var eventArgs = new LogMessageReceivedEventArgs( sequence, logLevel, date, sourceName, loggerMessage, extendedData, callerMemberName, callerFilePath, callerLineNumber); foreach (var logger in Loggers) { Task.Run(() => { if (logger.LogLevel <= logLevel) { logger.Log(eventArgs); } }); } }
/// <inheritdoc/> public void Log(LogMessageReceivedEventArgs logEvent) { var(outputMessage, _) = GetOutputAndColor(logEvent); System.Diagnostics.Debug.Write(outputMessage); }
/// <inheritdoc /> public void Log(LogMessageReceivedEventArgs logEvent) { ConsoleLogger.GetOutputAndColor(logEvent, true, out var outputMessage); _logQueue.Enqueue(outputMessage + Environment.NewLine); }
/// <inheritdoc /> public void Log(LogMessageReceivedEventArgs logEvent) { var(outputMessage, _) = GetOutputAndColor(logEvent); _logQueue.Enqueue(outputMessage); }