Example #1
0
 /// <summary>
 /// Calls <see cref="LogAction.Log(LogState)"/> with the given <see cref="LogState"/>.
 /// </summary>
 /// <param name="message">The message to pass to the <see cref="LogAction"/>.</param>
 /// <param name="ex">The <see cref="Exception"/> to pass to the <see cref="LogAction"/>.</param>
 /// <param name="level">The <see cref="LogLevel"/> of the message.</param>
 protected override void Log(
     LogLevel level,
     string message,
     Exception ex = null)
 {
     if (Level <= level)
     {
         StackFrame stackFrame = new StackTrace(2, true).GetFrame(0);
         string     filePath   = stackFrame.GetFileName();
         string     methodName = stackFrame.GetMethod().ToString();
         int        lineNumber = stackFrame.GetFileLineNumber();
         LogState   state      = new LogState(message, ex, methodName, filePath, lineNumber, level);
         Action.Log(state);
     }
 }
Example #2
0
        /// <summary>
        /// Returns a compact message from a given <see cref="LogState"/>.
        /// </summary>
        /// <param name="state">The <see cref="LogState"/> passed by the <see cref="Logger"/>.</param>
        /// <returns>A message based on a given <see cref="LogState"/>.</returns>
        public static string CompactMessage(LogState state)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"[{state.Time.ToString("MM/dd hh:mm")}] ");
            if (state.Exception != null)
            {
                sb.Append($@"{state.Exception.GetType().Name}  ");
            }
            if (!string.IsNullOrWhiteSpace(state.Message))
            {
                sb.Append(state.Message);
            }
            return(sb.AppendLine().ToString());
        }
Example #3
0
 /// <summary>
 /// Calls <see cref="LogAction.Log(LogState)"/> with the given <see cref="LogState"/>.
 /// </summary>
 /// <param name="message">The message to pass to the <see cref="LogAction"/>.</param>
 /// <param name="ex">The <see cref="Exception"/> to pass to the <see cref="LogAction"/>.</param>
 /// <param name="methodName">The calling method's name.</param>
 /// <param name="filePath">The file that this method was called.</param>
 /// <param name="lineNumber">The line number that this method was called.</param>
 /// <param name="level">The <see cref="LogLevel"/> of the message.</param>
 protected override void Log(
     string message,
     Exception ex,
     string methodName,
     string filePath,
     int lineNumber,
     LogLevel level)
 {
     if (Level <= level)
     {
         LogState state = new LogState(message, ex, methodName, filePath, lineNumber, level);
         lock (this) {
             Action.Log(state);
         }
     }
 }
Example #4
0
        /// <summary>
        /// Returns the default message from a given <see cref="LogState"/>.
        /// </summary>
        /// <param name="state">The <see cref="LogState"/> passed by the <see cref="Logger"/>.</param>
        /// <returns>A message based on a given <see cref="LogState"/>.</returns>
        public static string DefaultMessage(LogState state)
        {
            StringBuilder sb    = new StringBuilder();
            string        level = state.Level.ToString();

            sb.AppendLine($"[{state.Time.ToString("MM/dd/yyyy hh:mm:ss")} {level}] {Path.GetFileName(state.File)}::{state.Method}({state.Line})");
            if (state.Exception != null)
            {
                sb.AppendLine($@"{state.Exception.GetType().Name}  {state.Exception.Message}");
            }
            if (!string.IsNullOrWhiteSpace(state.Message))
            {
                sb.AppendLine(state.Message);
            }
            return(sb.ToString());
        }
        /// <summary>
        /// Writes the log message to the given <see cref="StringBuilder"/>.
        /// </summary>
        /// <param name="state">The <see cref="LogState"/>.</param>
        public override void Log(LogState state)
        {
            string msg = Compact ? CompactMessage(state) : DefaultMessage(state);

            Writer.Write(msg);
        }
Example #6
0
 /// <summary>
 /// The method that handles a call from a <see cref="Logger"/>.
 /// </summary>
 /// <param name="state">The <see cref="LogState"/> passed by the <see cref="Logger"/>.</param>
 public abstract void Log(LogState state);