Exemplo n.º 1
0
        private static object ToRawValue(LogEventPropertyValue logEventValue)
        {
            // Special-case a few types of LogEventPropertyValue that allow us to maintain better type fidelity.
            // For everything else take the default string rendering as the data.
            ScalarValue scalarValue = logEventValue as ScalarValue;

            if (scalarValue != null)
            {
                return(scalarValue.Value);
            }

            SequenceValue sequenceValue = logEventValue as SequenceValue;

            if (sequenceValue != null)
            {
                object[] arrayResult = sequenceValue.Elements.Select(e => ToRawScalar(e)).ToArray();
                if (arrayResult.Length == sequenceValue.Elements.Count)
                {
                    // All values extracted successfully, it is a flat array of scalars
                    return(arrayResult);
                }
            }

            StructureValue structureValue = logEventValue as StructureValue;

            if (structureValue != null)
            {
                IDictionary <string, object> structureResult = new Dictionary <string, object>(structureValue.Properties.Count);
                foreach (var property in structureValue.Properties)
                {
                    structureResult[property.Name] = ToRawScalar(property.Value);
                }

                if (structureResult.Count == structureValue.Properties.Count)
                {
                    if (structureValue.TypeTag != null)
                    {
                        structureResult["$type"] = structureValue.TypeTag;
                    }

                    return(structureResult);
                }
            }

            DictionaryValue dictionaryValue = logEventValue as DictionaryValue;

            if (dictionaryValue != null)
            {
                IDictionary <string, object> dictionaryResult = dictionaryValue.Elements
                                                                .Where(kvPair => kvPair.Key.Value is string)
                                                                .ToDictionary(kvPair => (string)kvPair.Key.Value, kvPair => ToRawScalar(kvPair.Value));
                if (dictionaryResult.Count == dictionaryValue.Elements.Count)
                {
                    return(dictionaryResult);
                }
            }

            // Fall back to string rendering of the value
            return(logEventValue.ToString());
        }
Exemplo n.º 2
0
 private static void AddEventPropertyAsLabel(TextWriter output, string eventPropertyKey, LogEventPropertyValue eventPropertyValue)
 {
     output.Write(eventPropertyKey);
     output.Write("=\\\"");
     output.Write(eventPropertyValue.ToString());
     output.Write("\\\"");
 }
 private static object GetPropertyValue(LogEventPropertyValue propertyValue)
 {
     return(propertyValue switch
     {
         SequenceValue sequenceValue => sequenceValue.Elements.Select(RenderSequenceValue).ToArray(),
         ScalarValue scalarValue => scalarValue.Value,
         _ => propertyValue.ToString()
     });
Exemplo n.º 4
0
        /// <summary>
        /// All Serilog property values are quoted, which is unnecessary, as we are going to encase them in
        /// quotes anyway, to conform to the specification for syslog structured data values - so this
        /// removes them and also unescapes any others
        /// </summary>
        private static string RenderPropertyValue(LogEventPropertyValue propertyValue)
        {
            // Trim surrounding quotes, and unescape all others
            var result = propertyValue
                         .ToString()
                         .TrimAndUnescapeQuotes();

            // Use a backslash to escape backslashes, double quotes and closing square brackets
            return(Regex.Replace(result, @"[\]\\""]", match => $@"\{match}"));
        }
Exemplo n.º 5
0
        protected string GetPropertyValue(LogEventPropertyValue value)
        {
            var scalar = value as ScalarValue;

            if (scalar?.Value != null)
            {
                return(scalar.Value.ToString());
            }

            return(value.ToString());
        }
Exemplo n.º 6
0
        private static object ToRawScalar(LogEventPropertyValue value)
        {
            ScalarValue scalarValue = value as ScalarValue;

            if (scalarValue != null)
            {
                return(scalarValue.Value);
            }

            return(value.ToString());
        }
Exemplo n.º 7
0
        private object AutoCorrectResponseStatus(LogEventPropertyValue value)
        {
            var statusCode = value.ToString();

            if (statusCode.All(char.IsNumber))
            {
                return(value);
            }

            Enum.TryParse <HttpStatusCode>(statusCode, out var result);
            return((int)result);
        }
Exemplo n.º 8
0
        static string ValueToString(LogEventPropertyValue property)
        {
            var scalarValue = property as ScalarValue;

            if (scalarValue != null)
            {
                var valueType = scalarValue.Value.GetType();
                if (valueType == typeof(DateTime) || valueType == typeof(DateTimeOffset))
                {
                    return(scalarValue.ToString("yyyy-MM-ddTHH:mm:ss.fffK", CultureInfo.InvariantCulture));
                }
            }
            return(property.ToString().Trim('"'));
        }
Exemplo n.º 9
0
 private static object GetValue(LogEventPropertyValue value, out GraylogPropertyType propType)
 {
     // as GELF currently only allows string or numeric values, we'll have to convert if type is not matching
     if (value == null)
     {
         propType = GraylogPropertyType.String;
         return(string.Empty);
     }
     if (!(value is ScalarValue scalar))
     {
         propType = GraylogPropertyType.String;
         return(value.ToString());
     }
     if (scalar.Value == null)
     {
         propType = GraylogPropertyType.String;
         return(value.ToString());
     }
     propType = GetPropertyType(scalar.Value);
     return(propType == GraylogPropertyType.Numeric
         ? scalar.Value
         : scalar.Value.ToString());
 }
Exemplo n.º 10
0
        private static object GetPropertyInternalValue(LogEventPropertyValue propertyValue)
        {
            switch (propertyValue)
            {
            case ScalarValue sv: return(GetInternalValue(sv.Value));

            case SequenceValue sv: return(sv.Elements.Select(GetPropertyInternalValue).ToArray());

            case DictionaryValue dv: return(dv.Elements.Select(kv => new { Key = kv.Key.Value, Value = GetPropertyInternalValue(kv.Value) }).ToDictionary(i => i.Key, i => i.Value));

            case StructureValue sv: return(sv.Properties.Select(kv => new { Key = kv.Name, Value = GetPropertyInternalValue(kv.Value) }).ToDictionary(i => i.Key, i => i.Value));
            }
            return(propertyValue.ToString());
        }
Exemplo n.º 11
0
        public static string ExtractStringRepresentationOfLogEventPropertyValue(
            LogEventPropertyValue property)
        {
            // We don't want to decorate strings we additional
            // unnecessary quotes, so we need to extract its value directly
            if (property is ScalarValue sv)
            {
                if (sv.Value is string s)
                {
                    return(s);
                }
            }

            var actualValue = property.ToString();

            return(actualValue);
        }
Exemplo n.º 12
0
        public LogEventVM(LogEvent logEvent, IFormatProvider formatProvider)
        {
            Level     = logEvent.Level;
            Time      = logEvent.Timestamp.ToString("HH:mm:ss.fff");
            Message   = logEvent.RenderMessage(formatProvider);
            Exception = logEvent.Exception?.Message;

            LogEventPropertyValue contextValue = null;

            if (logEvent.Properties.TryGetValue("SourceContext", out contextValue))
            {
                SourceContext = contextValue?.ToString().Replace("\"", "");
            }

            LogEventPropertyValue tokenValue = null;

            if (logEvent.Properties.TryGetValue("Token", out tokenValue))
            {
                Token = tokenValue?.ToString();
            }
        }
Exemplo n.º 13
0
        private object ToRawValue(LogEventPropertyValue logEventValue)
        {
            // Special-case a few types of LogEventPropertyValue that allow us to maintain better type fidelity.
            // For everything else take the default string rendering as the data.
            ScalarValue scalarValue = logEventValue as ScalarValue;

            if (scalarValue != null)
            {
                return(scalarValue.Value);
            }

            SequenceValue sequenceValue = logEventValue as SequenceValue;

            if (sequenceValue != null)
            {
                object[] arrayResult = sequenceValue.Elements.OfType <ScalarValue>().Select(e => e.Value).ToArray();
                if (arrayResult.Length == sequenceValue.Elements.Count)
                {
                    // All values extracted successfully, it is a flat array of scalars
                    return(arrayResult);
                }
            }

            DictionaryValue dictionaryValue = logEventValue as DictionaryValue;

            if (dictionaryValue != null)
            {
                IDictionary <string, object> dictionaryResult = dictionaryValue.Elements
                                                                .Where(kvPair => kvPair.Key.Value is string && kvPair.Value is ScalarValue)
                                                                .ToDictionary(kvPair => (string)kvPair.Key.Value, kvPair => ((ScalarValue)kvPair.Value).Value);
                if (dictionaryResult.Count == dictionaryValue.Elements.Count)
                {
                    return(dictionaryResult);
                }
            }

            // Fall back to string rendering of the value
            return(logEventValue.ToString());
        }
Exemplo n.º 14
0
        private static Process GetProcess(LogEvent e, bool mapFromCurrentThread)
        {
            LogEventPropertyValue processNameProp = null;
            LogEventPropertyValue processIdProp   = null;
            LogEventPropertyValue threadIdProp    = null;

            e.Properties.TryGetValue(SpecialKeys.ProcessName, out processNameProp);
            e.Properties.TryGetValue(SpecialKeys.ProcessId, out processIdProp);
            e.Properties.TryGetValue(SpecialKeys.ThreadId, out threadIdProp);
            if (processNameProp == null &&
                processIdProp == null &&
                threadIdProp == null &&
                !mapFromCurrentThread)
            {
                return(null);
            }

            var processName = processNameProp?.ToString();
            var processId   = processIdProp?.ToString();
            var threadId    = threadIdProp?.ToString();
            var pid         = int.TryParse(processId ?? "", out var p)
                                ? p
                                : (int?)null;

            if (!mapFromCurrentThread)
            {
                return(new Process
                {
                    Title = processName,
                    Name = processName,
                    Pid = pid,
                    Thread = int.TryParse(threadId ?? processId ?? "", out var id)
                                                ? new ProcessThread()
                    {
                        Id = id
                    }
                                                : null,
                });
Exemplo n.º 15
0
        private object GetRenderedProperty(LogEventPropertyValue value)
        {
            switch (value)
            {
            case SequenceValue sequenceValue:
                return(sequenceValue.Elements.Select(GetRenderedSequenceValue).ToArray());

            case StructureValue structureValue:
                return(structureValue.Properties
                       .ToDictionary(x => x.Name, x => GetRenderedProperty(x.Value)));

            case ScalarValue scalarValue:
                switch (scalarValue.Value)
                {
                case null:
                    return(null);

                case string str:
                    return(str);

                case DateTime dt:
                    return(dt.ToString("o"));

                case DateTimeOffset dt:
                    return(dt.ToString("o"));

                case var numericScalar when numericScalar.GetType().IsNumericType():
                    return(numericScalar);

                case var unknownScalar:
                    return(string.Format(_options.FormatProvider, "{0}", unknownScalar));
                }

            default:
                return(value.ToString(null, _options.FormatProvider));
            }
        }
Exemplo n.º 16
0
 private object GetRenderedSequenceValue(LogEventPropertyValue value) =>
 (value as ScalarValue)?.Value ?? value.ToString(null, _options.FormatProvider);
 public static string RemoveNonAlphanumerical(this LogEventPropertyValue initString) => _regex.Replace(initString.ToString(), string.Empty);
 /// <summary>
 /// Gets the value of the logged property value as a string.
 /// </summary>
 /// <param name="property">The property to get the value.</param>
 public static string ToStringValue(this LogEventPropertyValue property)
 {
     return(property.ToString().Trim('\"'));
 }