Beispiel #1
0
        /// <summary>
        /// Formats log event for text representation, not including any properties. Error is included though.
        /// </summary>
        public static string Format(LogEvent e, FormattedString format = null)
        {
            if (format == null) format = DefaultFormat;

             var b = new StringBuilder();

             foreach(Token token in format.Tokens)
             {
            switch(token.Type)
            {
               case TokenType.String:
                  b.Append(token.Value);
                  break;
               case TokenType.Parameter:
                  switch(token.Name)
                  {
                     case Time:
                        b.Append(e.EventTime.ToString(token.Format));
                        break;
                     case Severity:
                        string sev = e.Severity.ToString().ToUpper();
                        if (token.Format != null) sev = string.Format(token.NativeFormat, sev);
                        b.Append(sev);
                        break;
                     case Source:
                        b.Append(e.SourceName);
                        break;
                     case Message:
                        b.Append(e.FormattedMessage);
                        break;
                     case Error:
                        if (e.ErrorException != null)
                        {
                           b.AppendLine();
                           b.Append(e.ErrorException.ToString());
                        }
                        break;
                     case NewLine:
                        b.AppendLine();
                        break;
                     default:
                        if(e.Properties != null)
                        {
                           object value;
                           if(e.Properties.TryGetValue(token.Name, out value))
                           {
                              string custom = format.Format(token, value);
                              b.Append(custom);
                           }
                        }
                        break;
                  }
                  break;
            }
             }

             return b.ToString();
        }
        /// <summary>
        /// Constructs and instance of this class
        /// </summary>
        public PoshConsoleLogWriter(string format)
        {
            _format = format == null ? TextFormatter.DefaultFormat : FormattedString.Parse(format, null);

             Settings = new PoshConsoleLogWriterSettings
             {
            AbbreviateClassNames = false
             };

             Console.BackgroundColor = ConsoleColor.Black;
        }
Beispiel #3
0
      /// <summary>
      /// Creates an instance of file receiver
      /// </summary>
      /// <param name="fileName">Target filename. If file does not exist it will be created.</param>
      /// <param name="format">format</param>
      public FileLogWriter(string fileName, string format)
      {
         if(fileName == null) throw new ArgumentNullException(nameof(fileName));

         SplitPath(fileName, out _directoryName, out _fileNamePart, out _extensionPart);
         if(!Directory.Exists(_directoryName)) Directory.CreateDirectory(_directoryName);

         PreCreateDirectory(fileName);

         _format = format == null ? null : FormattedString.Parse(format, null);
      }
Beispiel #4
0
 /// <summary>
 /// Creates class instance
 /// </summary>
 public ConsoleLogWriter(string format)
 {
     _format = format == null ? null : FormattedString.Parse(format, null);
 }
Beispiel #5
0
 /// <summary>
 /// Creates an instance by specifying format string and parameters
 /// </summary>
 public static FormattedString Parse(string format, object[] parameters)
 {
     var fmt = new FormattedString(format, parameters);
      fmt.Parse();
      return fmt;
 }
Beispiel #6
0
        /// <summary>
        /// Formats log event for text representation, not including any properties. Error is included though.
        /// </summary>
        public static string Format(LogEvent e, FormattedString format = null)
        {
            if (format == null)
            {
                format = DefaultFormat;
            }

            var b = new StringBuilder();

            foreach (Token token in format.Tokens)
            {
                switch (token.Type)
                {
                case TokenType.String:
                    b.Append(token.Value);
                    break;

                case TokenType.Parameter:
                    switch (token.Name)
                    {
                    case Time:
                        b.Append(e.EventTime.ToString(token.Format));
                        break;

                    case Severity:
                        string sev = ToSeverityString(e);
                        if (token.Format != null)
                        {
                            sev = string.Format(token.NativeFormat, sev);
                        }
                        b.Append(sev);
                        break;

                    case Source:
                        b.Append(e.SourceName);
                        break;

                    case Message:
                        b.Append(e.FormattedMessage);
                        break;

                    case Error:
                        if (e.ErrorException != null)
                        {
                            b.AppendLine();
                            b.Append(e.ErrorException.ToString());
                        }
                        break;

                    case NewLine:
                        b.AppendLine();
                        break;

                    default:
                        if (e.Properties != null)
                        {
                            object value;
                            if (e.Properties.TryGetValue(token.Name, out value))
                            {
                                string custom = format.Format(token, value);
                                b.Append(custom);
                            }
                        }
                        break;
                    }
                    break;
                }
            }

            return(b.ToString());
        }