Пример #1
0
        /// <summary>
        /// Sends the chunk to posh console!
        /// </summary>
        private void Write(LogEvent e)
        {
            foreach (Token token in _format.Tokens)
            {
                switch (token.Type)
                {
                case TokenType.String:
                    Cg.Write(token.Value, SeparatorColour);
                    break;

                case TokenType.Parameter:
                    switch (token.Name)
                    {
                    case TextFormatter.Time:
                        Cg.Write(e.EventTime.ToString(token.Format), TimeColour);
                        break;

                    case TextFormatter.Severity:
                        WriteSeverity(e);
                        break;

                    case TextFormatter.Source:
                        Cg.Write(e.SourceName, SourceColour);
                        break;

                    case TextFormatter.Message:
                        LogMessage(e);
                        break;

                    case TextFormatter.Error:
                        LogError(e);
                        break;

                    case TextFormatter.NewLine:
                        Console.WriteLine();
                        break;

                    default:
                        if (e.Properties != null)
                        {
                            object value;
                            if (e.Properties.TryGetValue(token.Name, out value))
                            {
                                string custom = _format.Format(token, value);
                                Cg.Write(custom, ParameterColour);
                            }
                        }
                        break;
                    }
                    break;
                }
            }

            Console.WriteLine();

            if (_logProperties)
            {
                LogProperties(e);
            }
        }
Пример #2
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.ErrorException == null ? "I" : "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());
        }