public override string ToString() { StringBuilder output = new StringBuilder(string.Empty); output.AppendFormat("Provider: {0}|", Provider); output.AppendFormat("ID {0}|", Id.Code); output.AppendFormat("Value: {0} (0x{0:X})|", Id.Value); output.AppendFormat("Level: {0}|", Level.Name); output.AppendFormat("Version: {0}|", Version); if (!string.IsNullOrEmpty(Task.Name)) { output.AppendFormat("Task: {0}|", Task.Name); } if (!string.IsNullOrEmpty(Opcode.Name)) { output.AppendFormat("Opcode: {0}|", Opcode.Name); } if (Keywords != null && Keywords.Count > 0) { output.AppendFormat("Keywords: {0}|", CreateKeywordOutput(Keywords)); } if (!string.IsNullOrEmpty(LoggedTo.Name)) { output.AppendFormat("Logged To: {0}|", LoggedTo.Name); } if (!string.IsNullOrEmpty(Message)) { Message = FormattingTools.FormatMessageForPlaintext(Message); output.AppendFormat("Message: {0}|", Message); if (Parameters != null && Parameters.Count > 0) { output.AppendFormat("Parameters: {0}", CreateParameterOutput(Parameters)); } } string s = output.ToString().Trim(); if (s.EndsWith("|", true, CultureInfo.CurrentCulture)) { s = s.TrimEnd('|'); } return(s); }
public static string ToFormat(IList <EventData> events, string format) { string data = string.Empty; if (string.IsNullOrEmpty(format)) { format = "json"; } if (events != null && events.Any()) { switch (format.ToLower(CultureInfo.CurrentCulture)) { case "txt": StringBuilder txtBuilder = new StringBuilder(string.Empty); foreach (EventData eventData in events) { txtBuilder.AppendFormat("{0}{1}", eventData, Environment.NewLine); } data = txtBuilder.ToString(); break; case "json": JsonSerializerSettings settings = new JsonSerializerSettings { MaxDepth = int.MaxValue, Formatting = Formatting.None, }; data = JsonConvert.SerializeObject(events, settings); break; case "csv": CsvConfiguration config = new CsvConfiguration { AllowComments = false, DetectColumnCountChanges = true, IgnoreQuotes = true, QuoteAllFields = true, TrimFields = true }; StringBuilder csvBuilder = new StringBuilder(events.Count); using (StringWriter sw = new StringWriter(csvBuilder, CultureInfo.InvariantCulture)) { CsvWriter csvWriter = new CsvWriter(sw, config); foreach (EventData evt in events) { csvWriter.WriteField <string>(evt.Provider); csvWriter.WriteField <ushort>(evt.Id.Code); csvWriter.WriteField <string>(string.Format(CultureInfo.CurrentCulture, "0x{0:X}", evt.Id.Value)); csvWriter.WriteField <string>(evt.Level.Name); csvWriter.WriteField <byte>(evt.Version); csvWriter.WriteField <string>(evt.Task.Name); csvWriter.WriteField <string>(evt.Opcode.Name); csvWriter.WriteField <string>(CreateKeywordOutput(evt.Keywords)); csvWriter.WriteField <string>(evt.LoggedTo.Name); csvWriter.WriteField <string>(FormattingTools.FormatMessageForPlaintext(evt.Message)); csvWriter.WriteField <string>(CreateParameterOutput(evt.Parameters)); csvWriter.NextRecord(); } sw.Flush(); } csvBuilder.Insert(0, "\"Provider\",\"ID\",\"Value\",\"Level\",\"Version\",\"Task\",\"Opcode\",\"Keywords\",\"LoggedTo\",\"Message\",\"Parameters\"" + Environment.NewLine); data = csvBuilder.ToString(); break; default: break; } } return(data); }