internal static string JsonSerializePayload(IEnumerable<KeyValuePair<string, object>> payload) { try { using (var writer = new StringWriter(CultureInfo.InvariantCulture)) using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Transformalize.Libs.Newtonsoft.Json.Formatting.Indented, CloseOutput = false }) { jsonWriter.WriteStartObject(); foreach (var item in payload) { JsonWriteProperty(jsonWriter, item.Key, item.Value); } jsonWriter.WriteEndObject(); jsonWriter.Flush(); return writer.ToString(); } } catch (JsonWriterException jwe) { SemanticLoggingEventSource.Log.EventEntrySerializePayloadFailed(jwe.ToString()); var errorDictionary = new Dictionary<string, object> { { "Error", string.Format(CultureInfo.CurrentCulture, "JSON Serialization Error {0}", jwe.Message) } }; return JsonConvert.SerializeObject(errorDictionary, Transformalize.Libs.Newtonsoft.Json.Formatting.Indented); } }
public string Handle(Process[] processes) { var sw = new StringWriter(); var writer = new JsonTextWriter(sw); foreach (var process in processes) { writer.WriteStartArray(); foreach (var row in process.Results) { writer.WriteStartObject(); foreach (var alias in process.OutputFields().Aliases()) { writer.WritePropertyName(alias); writer.WriteValue(row[alias]); } writer.WriteEndObject(); } writer.WriteEndArray(); } writer.Flush(); return sw.ToString(); }
public static string LogsToJson(IEnumerable<string[]> logs) { var sw = new StringWriter(); var writer = new JsonTextWriter(sw); writer.WriteStartArray(); foreach (var log in logs) { if (log.Length <= 4) continue; writer.WriteStartObject(); //add writer.WritePropertyName("time"); writer.WriteValue(log[0]); writer.WritePropertyName("level"); writer.WriteValue(log[1].TrimEnd()); writer.WritePropertyName("process"); writer.WriteValue(log[2]); writer.WritePropertyName("entity"); writer.WriteValue(log[3]); writer.WritePropertyName("message"); writer.WriteValue(log[4].TrimEnd(new[] { ' ', '\r', '\n' })); writer.WriteEndObject(); //add } writer.WriteEndArray(); writer.Flush(); return sw.ToString(); }
public string MapFromAttributes() { var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) using (var jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); { var typeName = this.Infer.TypeName(this.TypeName); jsonWriter.WritePropertyName(typeName); jsonWriter.WriteStartObject(); { jsonWriter.WritePropertyName("properties"); jsonWriter.WriteStartObject(); { this.WriteProperties(jsonWriter); } jsonWriter.WriteEnd(); } jsonWriter.WriteEnd(); } jsonWriter.WriteEndObject(); return sw.ToString(); } }
/// <summary> /// Write the formatted event output. /// </summary> /// <param name="eventEntry">The event data to be formatted.</param> /// <param name="writer">The writer to receive the formatted output.</param> public void WriteEvent(EventEntry eventEntry, TextWriter writer) { Guard.ArgumentNotNull(eventEntry, "eventEntry"); using (var jsonWriter = new JsonTextWriter(writer) { CloseOutput = false, Formatting = this.formatting }) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName(PropertyNames.ProviderId); jsonWriter.WriteValue(eventEntry.ProviderId); jsonWriter.WritePropertyName(PropertyNames.EventId); jsonWriter.WriteValue(eventEntry.EventId); jsonWriter.WritePropertyName(PropertyNames.Keywords); jsonWriter.WriteValue((long)eventEntry.Schema.Keywords); jsonWriter.WritePropertyName(PropertyNames.Level); jsonWriter.WriteValue((int)eventEntry.Schema.Level); jsonWriter.WritePropertyName(PropertyNames.Message); jsonWriter.WriteValue(eventEntry.FormattedMessage); jsonWriter.WritePropertyName(PropertyNames.Opcode); jsonWriter.WriteValue((int)eventEntry.Schema.Opcode); jsonWriter.WritePropertyName(PropertyNames.Task); jsonWriter.WriteValue((int)eventEntry.Schema.Task); jsonWriter.WritePropertyName(PropertyNames.Version); jsonWriter.WriteValue(eventEntry.Schema.Version); jsonWriter.WritePropertyName(PropertyNames.Payload); EventEntryUtil.JsonWritePayload(jsonWriter, eventEntry); jsonWriter.WritePropertyName(PropertyNames.EventName); jsonWriter.WriteValue(eventEntry.Schema.EventName); jsonWriter.WritePropertyName(PropertyNames.Timestamp); jsonWriter.WriteValue(eventEntry.GetFormattedTimestamp(this.DateTimeFormat)); jsonWriter.WritePropertyName(PropertyNames.ProcessId); jsonWriter.WriteValue(eventEntry.ProcessId); jsonWriter.WritePropertyName(PropertyNames.ThreadId); jsonWriter.WriteValue(eventEntry.ThreadId); if (eventEntry.ActivityId != Guid.Empty) { jsonWriter.WritePropertyName(PropertyNames.ActivityId); jsonWriter.WriteValue(eventEntry.ActivityId); } if (eventEntry.RelatedActivityId != Guid.Empty) { jsonWriter.WritePropertyName(PropertyNames.RelatedActivityId); jsonWriter.WriteValue(eventEntry.RelatedActivityId); } jsonWriter.WriteEndObject(); if (IncludeEntrySeparator) { // Write an entry separator so all the logs can be read as an array, // adding the [] chars to the raw written data ( i.e: "[" + raw + "]" ) // where raw = {log1},{log2}, ... {logN}, jsonWriter.WriteRaw(EntrySeparator); } // Writes new line when indented if (jsonWriter.Formatting == Transformalize.Libs.Newtonsoft.Json.Formatting.Indented) { jsonWriter.WriteRaw("\r\n"); } } }