Example #1
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <param name="message">A string that contains the message to be logged.</param>
        /// <param name="level">A <see cref="LogLevel"/> enumeration value that specifies the event type.</param>
        /// <param name="eventId">An integer identifying the event that occurred.</param>
        /// <param name="callerData">Caller data information.</param>
        public virtual void WriteEntry(string message, LogLevel level, int eventId, CallerData callerData = null)
        {
            if (level > Level)
            {
                return;
            }

            ConsoleEventArgs e = new ConsoleEventArgs
            {
                Layout  = Layout,
                Message = message
            };

            ConsoleLogEntry?.Invoke(this, e);

            if (e.Handled)
            {
                return;
            }

            ConsoleMapping mapping = Layout.GetMapping(level) ?? ConsoleMapping.DefaultInfoMapping;

            IEnumerable <string> entries = mapping.Pattern;

            foreach (string entry in entries)
            {
                MappingInfo info = new MappingInfo {
                    Message = message, Caller = callerData, Level = level
                };
                Console.Write(info.Parse(entry));
            }
        }
Example #2
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <param name="message">A string that contains the message to be logged.</param>
        /// <param name="level">A <see cref="LogLevel"/> enumeration value that specifies the event type.</param>
        /// <param name="eventId">An integer identifying the event that occurred.</param>
        /// <param name="exception">Exception to log</param>
        /// <param name="callerData">Caller data information.</param>
        public void WriteExceptionEntry(string message, LogLevel level, int eventId, Exception exception, CallerData callerData = null)
        {
            if (level > Level)
            {
                return;
            }

            ConsoleEventArgs e = new ConsoleEventArgs
            {
                Layout  = Layout,
                Message = message
            };

            ConsoleLogEntry?.Invoke(this, e);

            if (e.Handled)
            {
                return;
            }

            // Gets mapping
            ConsoleMapping mapping = Layout.GetMapping(level) ?? ConsoleMapping.DefaultInfoMapping;

            // Gets entries to log
            List <string> entries = mapping.Pattern.ToList();

            // Log Message value
            string      messageEntry   = entries.FirstOrDefault();
            MappingInfo messageMapping = new MappingInfo {
                Message = message, Caller = callerData, Level = level
            };

            Console.Write(messageMapping.Parse(messageEntry));

            // Log Exeption
            entries.Remove(messageEntry);
            foreach (string entry in entries)
            {
                MappingInfo info = new MappingInfo {
                    Message = exception.Message, Exception = exception, Caller = callerData, Level = level
                };
                Console.Write(info.Parse(entry));
            }
        }