Beispiel #1
0
        /// <summary>
        /// Writes the event to the console.
        /// </summary>
        /// <param name="logEvent">The <see cref="LogEvent"/> instance to write.</param>
        protected override void WriteEventInternal(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            var writer = logEvent.EventSeverity >= LogEventSeverity.Error ? Console.Error : Console.Out;

            if (_LogEventFormatter != null)
            {
                Console.ForegroundColor = GetConsoleColor(logEvent.EventSeverity);
                _LogEventFormatter.FormatToTextWriter(logEvent, writer);
                Console.ResetColor();
            }
            else
            {
                writer.Write("[" + logEvent.DateTime.ToString("G", System.Globalization.CultureInfo.InvariantCulture) + "] ");
                Console.ForegroundColor = GetConsoleColor(logEvent.EventSeverity);
                writer.Write("[" + logEvent.EventSeverity.ToString() + "] ");
                Console.ResetColor();
                writer.Write("[" + logEvent.EventType.ToString() + "] ");
                writer.Write("[" + logEvent.Source + "] ");
                writer.Write("[" + logEvent.SourceMethod + "] ");
                writer.WriteLine(logEvent.EventName);
                if (logEvent.Exception != null)
                {
                    writer.Write(logEvent.Exception.ToString());
                }
            }
        }
Beispiel #2
0
        public void WriteBatch(IEnumerable <LogEvent> logEvents)
        {
            if (logEvents == null)
            {
                return;
            }

            var label = "Event Batch";

            if (_Formatter == null)
            {
                _Queue.Send(logEvents, label);
            }
            else
            {
                using (var pooledWriter = Globals.TextWriterPool.Take())
                {
                    var writer = pooledWriter.Value;

                    foreach (var logEvent in logEvents)
                    {
                        _Formatter.FormatToTextWriter(logEvent, writer);
                        writer.Flush();
                        if (writer.GetStringBuilder().Length > ThreeMegabytes)
                        {
                            _Queue.Send(writer.GetText(), label);
                            writer.Close();                             // Reset the writer to continue the batch.
                        }
                    }

                    //Send the last batch if any.
                    if (writer.GetStringBuilder().Length > 0)
                    {
                        _Queue.Send(writer.GetText(), label);
                    }
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Uses the configured formatter to write the supplied <see cref="LogEvent"/> instance to the stream.
 /// </summary>
 /// <param name="logEvent">The <see cref="LogEvent"/> instance to write.</param>
 protected override void WriteEventInternal(LogEvent logEvent)
 {
     _LogEventFormatter.FormatToTextWriter(logEvent, _Writer);
     _Writer.Flush();
 }