Example #1
0
        static void ToJson(TraceData traceData, TimeSpan currentOffset, TextWriter payload)
        {
            string level;

            if (!LevelMap.TryGetValue(traceData.EventType, out level))
            {
                level = "Verbose";
            }

            payload.Write("{");

            var parentCollection = new ArrayList();
            var delim            = "";

            WriteJsonProperty("Timestamp", traceData.DateTime, ref delim, payload, parentCollection);
            WriteJsonProperty("Level", level, ref delim, payload, parentCollection);

            WriteJsonProperty("MessageTemplate", traceData.MessageFormat ?? string.Empty, ref delim, payload, parentCollection);

            // First (if any) Exception found in the message args
            if (traceData.Exception != null)
            {
                WriteJsonProperty("Exception", traceData.Exception, ref delim, payload, parentCollection);
            }

            payload.Write(",\"Properties\":{");

            var pdelim   = "";
            var seenKeys = new List <string>();

            WriteJsonProperty(EventTypeKey, traceData.EventType, ref pdelim, payload, parentCollection);
            seenKeys.Add(EventTypeKey);

            if (traceData.Source != null)
            {
                WriteJsonProperty(SourceKey, traceData.Source, ref pdelim, payload, parentCollection);
                seenKeys.Add(SourceKey);
                WriteJsonProperty(EventIdKey, traceData.Id, ref pdelim, payload, parentCollection);
                seenKeys.Add(EventIdKey);
            }

            WriteJsonProperty(ActivityIdKey, traceData.ActivityId, ref pdelim, payload, parentCollection);
            seenKeys.Add(ActivityIdKey);

            if (traceData.RelatedActivityId.HasValue)
            {
                WriteJsonProperty(RelatedActivityIdKey, traceData.RelatedActivityId, ref pdelim, payload, parentCollection);
                seenKeys.Add(RelatedActivityIdKey);
            }

            if (traceData.Data != null && traceData.Data.Count > 0)
            {
                WriteJsonProperty("Data", traceData.Data, ref pdelim, payload, parentCollection);
                seenKeys.Add("Data");
            }

            if (traceData.MessageArgs != null)
            {
                for (var i = 0; i < traceData.MessageArgs.Count; ++i)
                {
                    var argKey = i.ToString(CultureInfo.InvariantCulture);
                    WriteJsonProperty(argKey, traceData.MessageArgs[i], ref pdelim, payload, parentCollection);
                    seenKeys.Add(argKey);
                }
            }

            if (traceData.Properties != null)
            {
                foreach (var property in traceData.Properties)
                {
                    var sanitizedKey = SanitizeKey(property.Key.ToString());
                    if (seenKeys.Contains(sanitizedKey))
                    {
                        continue;
                    }

                    seenKeys.Add(sanitizedKey);
                    WriteJsonProperty(sanitizedKey, property.Value, ref pdelim, payload, parentCollection);
                }
            }

            payload.Write("}");
            payload.Write("}");
        }