Exemplo n.º 1
0
        private void WriteEntryCore(string message, string source, ConsoleLogEntryType type, DateTimeOffset createdTime, ConsoleLogEntryStyle style)
        {
            if (style == null)
            {
                style = GetDefaultEntryStyle(type);
            }

            lock (consoleSyncRoot)
            {
                var originalBackgroundColor = Console.BackgroundColor;
                var originalForegroundColor = Console.ForegroundColor;

                try
                {
                    if (style.BackgroundColor != null)
                    {
                        Console.BackgroundColor = (ConsoleColor)style.BackgroundColor;
                    }

                    if (style.ForegroundColor != null)
                    {
                        Console.ForegroundColor = (ConsoleColor)style.ForegroundColor;
                    }

                    if (style.MessageFormat != null)
                    {
                        writer.WriteLine(style.MessageFormat, source, message, type, createdTime);
                    }
                    else
                    {
                        if (source != null)
                        {
                            writer.Write(source);
                            writer.Write(": ");
                        }

                        writer.WriteLine(message);
                    }

                    writer.Flush();
                }
                finally
                {
                    if (style.BackgroundColor != null)
                    {
                        Console.ForegroundColor = originalForegroundColor;
                    }

                    if (style.ForegroundColor != null)
                    {
                        Console.BackgroundColor = originalBackgroundColor;
                    }
                }
            }
        }
        /// <summary>
        /// Determines if a <see cref="ConsoleLogEntryType"/> value is valid.
        /// </summary>
        /// <param name="value">The <see cref="ConsoleLogEntryType"/> value to test.</param>
        /// <returns>true if the <see cref="ConsoleLogEntryType"/> value is valid;
        /// otherwise false.</returns>
        public static bool IsValid(this ConsoleLogEntryType value)
        {
            switch (value)
            {
            case ConsoleLogEntryType.None:
            case ConsoleLogEntryType.Information:
            case ConsoleLogEntryType.Warning:
            case ConsoleLogEntryType.Error:
                return(true);

            default:
                return(false);
            }
        }
Exemplo n.º 3
0
        private ConsoleLogEntryStyle GetDefaultEntryStyle(ConsoleLogEntryType type)
        {
            switch (type)
            {
            case ConsoleLogEntryType.Information:
                return(DefaultStyle.InformationStyle);

            case ConsoleLogEntryType.Warning:
                return(DefaultStyle.WarningStyle);

            case ConsoleLogEntryType.Error:
                return(DefaultStyle.ErrorStyle);

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }
        }
Exemplo n.º 4
0
        public void WriteEntry(string message, string source, ConsoleLogEntryType type, DateTimeOffset createdTime, ConsoleLogEntryStyle style)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (!type.IsValid())
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            WriteEntryCore(message, source, type, createdTime, style);
        }
        public ConsoleLogEntry(string source, string message, ConsoleLogEntryType entryType, DateTimeOffset createdTime)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!entryType.IsValid())
            {
                throw new ArgumentOutOfRangeException(nameof(entryType));
            }

            Source      = source;
            Message     = message;
            EntryType   = entryType;
            CreatedTime = createdTime;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Writes an entry to the console.
 /// </summary>
 public virtual void WriteEntry(string message, string source, ConsoleLogEntryType type, DateTimeOffset createdTime)
 {
     WriteEntry(message, source, type, createdTime, null);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Writes an entry to the console.
 /// </summary>
 public void WriteEntry(string message, string source, ConsoleLogEntryType type)
 {
     WriteEntry(message, source, type, DateTimeOffset.Now, null);
 }
 public ConsoleLogEntry(string source, string message, ConsoleLogEntryType entryType)
     : this(source, message, entryType, DateTimeOffset.Now)
 {
 }