/// <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; } ColoredConsoleEventArgs e = new ColoredConsoleEventArgs { Layout = Layout, Message = message }; ColoredConsoleLogEntry?.Invoke(this, e); if (e.Handled) { return; } ColoredConsoleMapping mapping = Layout.GetMapping(level) ?? ColoredConsoleMapping.DefaultInfoMapping; Console.ForegroundColor = mapping.ForeColor; Console.BackgroundColor = mapping.BackColor; 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)); } }
/// <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 virtual void WriteExceptionEntry(string message, LogLevel level, int eventId, Exception exception, CallerData callerData = null) { if (level > Level) { return; } // Gets mapping FileMapping mapping = Layout.GetMapping(level) ?? FileMapping.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 }; WriteMessage(messageMapping.Parse(messageEntry)); // Log Exeption entries.Remove(messageEntry); StringBuilder builder = new StringBuilder(); foreach (string entry in entries) { MappingInfo info = new MappingInfo { Message = exception.Message, Exception = exception, Caller = callerData, Level = level }; builder.Append(info.Parse(entry)); } WriteMessage(builder.ToString()); }
/// <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; } ColoredConsoleEventArgs e = new ColoredConsoleEventArgs { Layout = Layout, Message = message }; ColoredConsoleLogEntry?.Invoke(this, e); if (e.Handled) { return; } // Gets mapping ColoredConsoleMapping mapping = Layout.GetMapping(level) ?? ColoredConsoleMapping.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.ForegroundColor = ColoredConsoleMapping.DefaultInfoMapping.ForeColor; Console.BackgroundColor = ColoredConsoleMapping.DefaultInfoMapping.BackColor; Console.Write(messageMapping.Parse(messageEntry)); // Log Exeption entries.Remove(messageEntry); Console.ForegroundColor = mapping.ForeColor; Console.BackgroundColor = mapping.BackColor; foreach (string entry in entries) { MappingInfo info = new MappingInfo { Message = exception.Message, Exception = exception, Caller = callerData, Level = level }; Console.Write(info.Parse(entry)); } }
/// <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; } StringBuilder builder = new StringBuilder(); FileMapping mapping = Layout.GetMapping(level) ?? FileMapping.DefaultInfoMapping; IEnumerable <string> entries = mapping.Pattern; foreach (string entry in entries) { MappingInfo info = new MappingInfo { Message = message, Caller = callerData, Level = level }; builder.Append(info.Parse(entry)); } WriteMessage(builder.ToString()); }