Exemplo n.º 1
0
        public static void FormatDictionaryValue(LogEvent logEvent, TextWriter builder, DictionaryValue value, string propertyName)
        {
            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "{");

            var isFirst = true;

            foreach (var element in value.Elements)
            {
                if (!isFirst)
                {
                    ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, ", ");
                }

                isFirst = false;

                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "[");

                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.StringValue))
                {
                    Format(logEvent, element.Key, builder, null, propertyName);
                }

                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "]=");

                Format(logEvent, element.Value, builder, null, propertyName);
            }

            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "}");
        }
Exemplo n.º 2
0
        private static void WriteLevel(LogEvent logEvent, TextWriter builder)
        {
            var text = logEvent.Level switch
            {
                LogEventLevel.Verbose => "VRB",
                LogEventLevel.Debug => "DBG",
                LogEventLevel.Information => "INF",
                LogEventLevel.Warning => "WRN",
                LogEventLevel.Error => "ERR",
                LogEventLevel.Fatal => "FTL",
                _ => null,
            };

            var colorCode = logEvent.Level switch
            {
                LogEventLevel.Verbose => ColorCode.LvlTokenVrb,
                LogEventLevel.Debug => ColorCode.LvlTokenDbg,
                LogEventLevel.Information => ColorCode.LvlTokenInf,
                LogEventLevel.Warning => ColorCode.LvlTokenWrn,
                LogEventLevel.Error => ColorCode.LvlTokenErr,
                LogEventLevel.Fatal => ColorCode.LvlTokenFtl,
                _ => ColorCode.LvlTokenInf,
            };

            ColorCodeContext.Write(builder, colorCode, text);
        }
Exemplo n.º 3
0
        public static void FormatStructureValue(LogEvent logEvent, TextWriter builder, StructureValue value)
        {
            if (value.TypeTag != null)
            {
                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.StructureName, value.TypeTag + " ");
            }

            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "{");

            var isFirst = true;

            foreach (var property in value.Properties)
            {
                if (!isFirst)
                {
                    ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, ", ");
                }

                isFirst = false;

                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.StructureName, property.Name);
                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "=");

                Format(logEvent, property.Value, builder, null, property.Name);
            }

            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "}");
        }
Exemplo n.º 4
0
 private static void WriteTimeStamp(LogEvent logEvent, TextWriter builder, string format)
 {
     using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.TimeStamp_Property_Exception))
     {
         new ScalarValue(logEvent.Timestamp).Render(builder, format);
     }
 }
Exemplo n.º 5
0
        private static void WriteMessage(LogEvent logEvent, TextWriter builder)
        {
            foreach (var token in logEvent.MessageTemplate.Tokens)
            {
                switch (token)
                {
                case TextToken tt:
                {
                    ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Message_Exception, tt.Text);
                    break;
                }

                case PropertyToken pt:
                {
                    if (!logEvent.Properties.TryGetValue(pt.PropertyName, out var value))
                    {
                        ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.TimeStamp_Property_Exception, pt.ToString());
                    }
                    else
                    {
                        ValueFormatter.Format(logEvent, value, builder, pt.Format, pt.PropertyName);
                    }

                    break;
                }
                }
            }
        }
Exemplo n.º 6
0
        private static void WriteProperty(LogEvent logEvent, TextWriter builder, string propertyName, string format)
        {
            if (!logEvent.Properties.TryGetValue(propertyName, out var propertyValue))
            {
                return;
            }

            using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.TimeStamp_Property_Exception))
            {
                propertyValue.Render(builder, format);
            }
        }
Exemplo n.º 7
0
        private static void WriteException(LogEvent logEvent, TextWriter builder)
        {
            if (logEvent.Exception == null)
            {
                return;
            }

            var lines = logEvent.Exception.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                var colorCode = line.StartsWith("   ", StringComparison.InvariantCultureIgnoreCase)
                    ? ColorCode.TimeStamp_Property_Exception
                    : ColorCode.Message_Exception;

                ColorCodeContext.Write(builder, colorCode, line + Environment.NewLine);
            }
        }
Exemplo n.º 8
0
        private static void FormatSequenceValue(LogEvent logEvent, TextWriter builder, SequenceValue sequence, string propertyName)
        {
            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "[");

            var isFirst = true;

            foreach (var element in sequence.Elements)
            {
                if (!isFirst)
                {
                    ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, ", ");
                }

                isFirst = false;
                Format(logEvent, element, builder, null, propertyName);
            }

            ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, "]");
        }
Exemplo n.º 9
0
        public static void FormatScalarValue(LogEvent logEvent, TextWriter builder, ScalarValue value, string format, string propertyName)
        {
            switch (value.Value)
            {
            case null:
                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.NullValue, "NULL");
                break;

            case string strv:
                if (string.IsNullOrEmpty(propertyName) || !CustomColoredProperties.Map.TryGetValue(propertyName, out var colorCode))
                {
                    colorCode = ColorCode.StringValue;
                }

                using (ColorCodeContext.StartOverridden(builder, logEvent, colorCode))
                {
                    builder.Write(strv);
                    break;
                }

            case bool bv:
                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.BooleanValue, bv ? "true" : "false");
                break;

            case char chv:
                ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.ScalarValue, "\'" + chv.ToString(CultureInfo.InvariantCulture) + "\'");
                break;

            case sbyte _:
            case byte _:
            case short _:
            case ushort _:
            case int _:
            case uint _:
            case long _:
            case ulong _:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.NumberValue))
                {
                    if (string.IsNullOrEmpty(format))
                    {
                        value.Render(builder, "#,0", CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        value.Render(builder, format, CultureInfo.InvariantCulture);
                    }

                    break;
                }

            case float _:
            case double _:
            case decimal _:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.NumberValue))
                {
                    if (string.IsNullOrEmpty(format))
                    {
                        value.Render(builder, "#,0.#", CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        value.Render(builder, format, CultureInfo.InvariantCulture);
                    }

                    break;
                }

            case TimeSpan ts:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.TimeSpanValue))
                {
                    if (string.IsNullOrEmpty(format))
                    {
                        if (ts.Days > 0)
                        {
                            value.Render(builder, @"d\.hh\:mm");
                        }
                        else if (ts.Hours > 0)
                        {
                            value.Render(builder, @"h\:mm\:ss", CultureInfo.InvariantCulture);
                        }
                        else if (ts.Minutes > 0)
                        {
                            value.Render(builder, @"m\:ss", CultureInfo.InvariantCulture);
                        }
                        else if (ts.Seconds > 0)
                        {
                            value.Render(builder, @"s\.fff", CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            value.Render(builder, @"\.fff", CultureInfo.InvariantCulture);
                        }
                    }
                    else
                    {
                        value.Render(builder, format, CultureInfo.InvariantCulture);
                    }

                    break;
                }

            case DateTime _:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.ScalarValue))
                {
                    value.Render(builder, "yyyy.MM.dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
                }

                break;

            case DateTimeOffset _:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.ScalarValue))
                {
                    value.Render(builder, "yyyy.MM.dd HH:mm:ss.fff zzz", CultureInfo.InvariantCulture);
                }

                break;

            default:
                using (ColorCodeContext.StartOverridden(builder, logEvent, ColorCode.ScalarValue))
                {
                    value.Render(builder, format, CultureInfo.InvariantCulture);
                }

                break;
            }
        }
Exemplo n.º 10
0
 private static void WriteText(LogEvent logEvent, TextWriter builder, string text)
 {
     ColorCodeContext.WriteOverridden(builder, logEvent, ColorCode.Value, text);
 }