Esempio n. 1
0
        /// <summary>
        /// Writes the log to the output streams if the level is lower or equal to the set logging level.
        /// <para>This function is thread-safe due to it's stream locking.</para>
        /// </summary>
        /// <param name="level">A <see cref="Level"/> message level.</param>
        /// <param name="message">The value to write.</param>
        /// <param name="stack">The stacktrace to reference in the log record.</param>
        /// <param name="includeStackTrace">Set whether to include a full stacktrace in the log record.</param>
        private void Write(Level level, object message, StackTrace stack, bool includeStackTrace)
        {
            if (Silent)
            {
                return;
            }
            if (disposedValue)
            {
                throw new ObjectDisposedException(ToString());
            }

            foreach (var logger in Children)
            {
                logger.Write(level, message, stack, includeStackTrace);
            }
            if (LogLevel.Value < level.Value)
            {
                return;
            }

            // Get the formatted log record
            var record = GetRecord(level, message?.ToString(), stack, includeStackTrace);

            // Write the record to the the Trace class if no outputstreams are available
            if (!OutputStreams.Any())
            {
                System.Diagnostics.Trace.WriteLine(record);
            }
            else
            {
                // Write the log record to every stream
                foreach (var stream in OutputStreams)
                {
                    lock (stream)
                    {
                        if (UseConsoleHighlighting && stream == Console.Out)
                        {
                            WriteConsoleRecord(record);
                        }
                        else
                        {
                            stream.WriteLine(record);
                        }
                        stream.Flush();
                    }
                }
            }
        }