void ILoggingListener.Write(LogEntry entry)
        {
            if (_eventLog == null)
            {
                return;
            }

            EventLogEntryType type = EventLogEntryType.Information;
            switch (entry.MessageType)
            {
                case LogType.None:
                case LogType.Console:
                case LogType.Trace:
                case LogType.Debug:
                case LogType.Info:
                default:
                    type = EventLogEntryType.Information;
                    break;
                case LogType.Warning:
                    type = EventLogEntryType.Warning;
                    break;
                case LogType.Error:
                case LogType.Exception:
                    type = EventLogEntryType.Error;
                    break;
            }

            _eventLog.WriteEntry(entry.Message, type);
            // If the entry is an exception, write a separate entry for this case
            if (entry.Exception != null)
            {
                _eventLog.WriteEntry(entry.Exception.Message, EventLogEntryType.Error);
            }
        }
Пример #2
0
        private static void LoggingListener(LogEntry entry)
        {
            ConsoleColor back = ConsoleColor.Black;
            ConsoleColor fore = ConsoleColor.White;

            switch (entry.MessageType)
            {
                case LogType.None:
                case LogType.Info:
                    break;
                case LogType.Console:
                    fore = ConsoleColor.Blue;
                    break;
                case LogType.Debug:
                    fore = ConsoleColor.Cyan;
                    break;
                case LogType.Error:
                    fore = ConsoleColor.DarkYellow;
                    break;
                case LogType.Exception:
                    fore = ConsoleColor.Red;
                    break;
                case LogType.Trace:
                    fore = ConsoleColor.Gray;
                    break;
                case LogType.Warning:
                    fore = ConsoleColor.Yellow;
                    break;
                default:
                    break;
            }

            Console.BackgroundColor = back;
            Console.ForegroundColor = fore;
            Console.WriteLine("{0}", entry.Message);
        }
Пример #3
0
        private void LogCore(LogEntry entry)
        {
            bool isConsole = (entry.MessageType == LogType.Console);
            bool isNothing = (entry.MessageType == LogType.None);

            #if !DEBUG
            // ignore designated DEBUG-only messages (they might contain information that the user does not necessarily need to know)
            // however this does not apply if the user wants these messages to be logged (this might be due to a debug switch argument)
            if (entry.MessageType == LoggingMessageType.Debug && !ForceDebugMessageLogging)
            {
                return;
            }
            #endif
            // is logging to output file enabled?
            // this is only allowed when this is no console message
            if (!isConsole)
            {
                // To Cache, or not To Cache, that's the question
                if ((_cacheSize > 0)
                    && (_cacheCurrent.Count <= _cacheSize))
                {
                    _cacheCurrent.Add(entry);
                }
                else
                {
                    _cacheCurrent.Add(entry);
                    Flush();
                }
            }

            if (!isNothing)
            {
                // update our listeners
                // this goes asynchronous
                ThreadPool.QueueUserWorkItem(o =>
                {
                    // Create a copy of the listeners to avoid having a lock
                    var copy = _listeners.ToArray();

                    foreach (ILoggingListener listener in copy)
                    {
                        try
                        {
                            listener.Write(entry);
                        }
                        catch (Exception ex)
                        {
                            // Some listeners may not be robust enough. Trace information about the culprit.
                            // Then remove him from the list to avoid more errors.
                            _listeners.Remove(listener);

                            LogFormat(LogType.Error, this, "The log listener with type '{0}' caused an exception while writing. The logger will be disabled. Please see the log file.", listener.GetType().Name);
                            LogException(this, ex);
                        }
                    }
                });
            }
        }
Пример #4
0
 /// <summary>
 /// Logs a <see cref="Exception"/>.
 /// </summary>
 /// <param name="source">The component from which this type comes. The type name of the instance is used.</param>
 /// <param name="exception">The exception.</param>
 public void LogException(object source, Exception exception)
 {
     LogEntry entry = new LogEntry(LogType.Exception, GetLogSourceName(source), exception.Message);
     entry.Exception = exception;
     LogCore(entry);
 }
Пример #5
0
        private void LogCore(LogEntry entry)
        {
            if (_log == null)
            {
                return;
            }

            ILogger logger = _log.Logger;

            LoggingEventData data = new LoggingEventData();
            data.TimeStamp = entry.Timestamp;
            data.Message = entry.Message;
            data.Level = GetLog4netLevel(entry.MessageType);
            data.LoggerName = _log.Logger.Name;

            if (entry.Exception != null)
            {
                data.ExceptionString = entry.Exception.ToString();
            }

            LoggingEvent le = new LoggingEvent(data);
            le.Properties["Source"] = entry.Source;

            logger.Log(le);
        }
 /// <summary>
 /// Writes the given <see cref="LogEntry"/> and performs implementation-specific actions according to it.
 /// </summary>
 /// <param name="entry">The log entry to write.</param>
 protected virtual void Write(LogEntry entry)
 {
     WriteAction(entry);
 }
 void ILoggingListener.Write(LogEntry entry)
 {
     this.Write(entry);
 }
Пример #8
0
 /// <summary>
 /// Logs formatted text.
 /// </summary>
 /// <param name="type">The message type.</param>
 /// <param name="source">The component from which this type comes. The type name of the instance is used.</param>
 /// <param name="format">The text to use as the format string.</param>
 /// <param name="exception">The exception to log. If this is not null, it will create a separate entry just as "LogException" does.</param>
 /// <param name="arguments">The arguments to use for the format string.</param>
 public void LogFormat(LogType type, object source, string format, Exception exception, params object[] arguments)
 {
     LogEntry entry = new LogEntry(type, GetLogSourceName(source), String.Format(CultureInfo.InvariantCulture, format, arguments));
     entry.Exception = exception;
     LogCore(entry);
 }
Пример #9
0
        /// <summary>
        /// Creates the log entry from its string representation.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static LogEntry CreateFromString(string value)
        {
            LogEntry entry = new LogEntry();
            string[] tokens = value.Split(';');
            if (tokens.Length != 4)
            {
                // malformed
                return null;
            }
            entry.MessageType = entry.GetMessageType(tokens[0]);
            entry.Timestamp = DateTime.Parse(tokens[1], CultureInfo.InvariantCulture);
            entry.Source = tokens[2];
            entry.Message = tokens[3];

            return entry;
        }
Пример #10
0
        private void LogCore(LogEntry entry)
        {
            bool isConsole = (entry.MessageType == LogType.Console);
            bool isNothing = (entry.MessageType == LogType.None);

            #if !DEBUG
            // ignore designated DEBUG-only messages (they might contain information that the user does not necessarily need to know)
            // however this does not apply if the user wants these messages to be logged (this might be due to a debug switch argument)
            if (entry.MessageType == LoggingMessageType.Debug && !ForceDebugMessageLogging)
            {
                return;
            }
            #endif
            // is logging to output file enabled?
            // this is only allowed when this is no console message
            if (!isConsole)
            {
                // To Cache, or not To Cache, that's the question
                if ((_cacheSize > 0)
                    && (_cacheCurrent.Count <= _cacheSize))
                {
                    _cacheCurrent.Add(entry);
                }
                else
                {
                    _cacheCurrent.Add(entry);
                    Flush();
                }
            }

            if (!isNothing)
            {
                // update our listeners
                // this goes asynchronous
                ThreadPool.QueueUserWorkItem(o =>
                {
                    // Create a copy of the listeners to avoid having a lock
                    var copy = _listeners;

                    foreach (ILoggingListener listener in copy)
                    {
                        listener.Write(entry);
                    }
                });
            }
        }