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.WriteStartArray(); foreach (var alias in process.OutputFields().Aliases()) { writer.WriteStartObject(); writer.WritePropertyName("key"); writer.WriteValue(alias); writer.WritePropertyName("value"); writer.WriteValue(row[alias]); writer.WriteEndObject(); } writer.WriteEndArray(); } 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(); }
/// <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"); } } }