public override string ToString() { StringBuilder output = new StringBuilder(string.Empty); output.AppendFormat("Log: {0}|", Log); output.AppendFormat("Source: {0}|", Source); output.AppendFormat("ID: {0}|", Id.Code); output.AppendFormat("Value: {0} (0x{0:X})|", Id.Value); if (!string.IsNullOrEmpty(Message)) { Message = FormattingTools.FormatMessageForPlaintext(Message); output.AppendFormat("Message: {0}|", Message); } string s = output.ToString().Trim(); if (s.EndsWith("|", true, CultureInfo.CurrentCulture)) { s = s.TrimEnd(new char[] { '|' }); } return(s); }
public static string ToFormat(IList <ClassicEventData> events, string format) { string data = string.Empty; if (string.IsNullOrEmpty(format)) { format = "json"; } if (events != null && events.Count > 0) { switch (format.ToLower(CultureInfo.CurrentCulture)) { case "txt": StringBuilder txtBuilder = new StringBuilder(string.Empty); foreach (ClassicEventData eventData in events) { txtBuilder.AppendFormat("{0}{1}", eventData, Environment.NewLine); } data = txtBuilder.ToString(); break; case "json": JsonSerializerSettings settings = new JsonSerializerSettings { Formatting = Formatting.None, MaxDepth = int.MaxValue }; 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 (ClassicEventData evt in events) { csvWriter.WriteField <string>(evt.Log); csvWriter.WriteField <string>(evt.Source); csvWriter.WriteField <ushort>(evt.Id.Code); csvWriter.WriteField <string>(string.Format(CultureInfo.CurrentCulture, "0x{0:X}", evt.Id.Value)); csvWriter.WriteField <string>(FormattingTools.FormatMessageForPlaintext(evt.Message)); csvWriter.NextRecord(); } sw.Flush(); } csvBuilder.Insert(0, "\"Log\",\"Source\",\"ID\",\"Value\",\"Message\"" + Environment.NewLine); data = csvBuilder.ToString(); break; default: break; } } return(data); }