Beispiel #1
0
        public static Dictionary <string, object> ToDictionary(LogEventInfo source)
        {
            var timestampUtcIso8601 = Convert.ToUtcIso8601(source.TimeStamp);

            var result = new Dictionary <string, object>
            {
                { "TimeStamp", timestampUtcIso8601 },
                { "Level", source.Level.ToString() },
                { "LoggerName", source.LoggerName }
            };

            var hasTemplate = !string.Equals(source.Message, source.FormattedMessage, StringComparison.Ordinal);

            if (hasTemplate)
            {
                result.Add("Message", source.FormattedMessage);
                result.Add("MessageTemplate", source.Message);
            }
            else
            {
                result.Add("Message", source.Message);
            }

            if (source.Exception != null)
            {
                result.Add("Exception", source.Exception.ToString());
                result.Add("ExceptionType", source.Exception.GetType().Name);
                result.Add("ExceptionMessage", source.Exception.Message);
                result.Add("ExceptionStackTrace", source.Exception.StackTrace);
                result.Add("ExceptionFingerprint", ConvertException.ToFingerprint(source.Exception));
            }

            if (!hasTemplate && source.Parameters != null)
            {
                result.Add("Parameters", string.Join(",", source.Parameters.Select(Convert.ValueAsString)));
            }

            if (source.StackTrace != null)
            {
                result.Add("CallSite", StackHelper.CallSiteName(source.StackTrace));
            }

            if (source.HasProperties)
            {
                HarvestToDictionary(source.Properties, result, "data_");
            }

            if (source.Exception?.Data?.Count > 0)
            {
                HarvestToDictionary(source.Exception.Data, result, "ex_");
            }

            return(result);
        }
        public static Dictionary <string, object> ToDictionary(LogEventInfo source)
        {
            var timestampUtcIso8601 = Convert.ToUtcIso8601(source.TimeStamp);

            var result = new Dictionary <string, object>
            {
                { "TimeStamp", timestampUtcIso8601 },
                { "Level", source.Level.ToString() },
                { "LoggerName", source.LoggerName },
                { "Message", source.FormattedMessage },
            };

            if (source.Exception != null)
            {
                result.Add("Exception", source.Exception.ToString());
                result.Add("ExceptionType", source.Exception.GetType().Name);
                result.Add("ExceptionMessage", source.Exception.Message);
                result.Add("ExceptionStackTrace", source.Exception.StackTrace);
                result.Add("ExceptionFingerprint", ConvertException.ToFingerprint(source.Exception));
            }

            if (source.Parameters != null)
            {
                result.Add("Parameters", string.Join(",", source.Parameters.Select(x => x.ToString())));
            }

            if (source.StackTrace != null)
            {
                result.Add("CallSite", StackHelper.CallSiteName(source.StackTrace));
            }

            HarvestToDictionary(source.Properties, result, "data_");


            if (source.Exception != null)
            {
                HarvestToDictionary(source.Exception.Data, result, "ex_");
            }

            return(result);
        }