private string LogEventToJson(LogEvent logEvent) { if (ColumnOptions.LogEvent.ExcludeAdditionalProperties) { var filteredProperties = logEvent.Properties.Where(p => !AdditionalDataColumnNames.Contains(p.Key)); logEvent = new LogEvent(logEvent.Timestamp, logEvent.Level, logEvent.Exception, logEvent.MessageTemplate, filteredProperties.Select(x => new LogEventProperty(x.Key, x.Value))); } var sb = new StringBuilder(); using (var writer = new System.IO.StringWriter(sb)) JsonFormatter.Format(logEvent, writer); return(sb.ToString()); }
private string ConvertPropertiesToXmlStructure(IEnumerable <KeyValuePair <string, LogEventPropertyValue> > properties) { var options = ColumnOptions.Properties; if (options.ExcludeAdditionalProperties) { properties = properties.Where(p => !AdditionalDataColumnNames.Contains(p.Key)); } if (options.PropertiesFilter != null) { try { properties = properties.Where(p => options.PropertiesFilter(p.Key)); } catch (Exception ex) { SelfLog.WriteLine("Unable to filter properties to store in {0} due to following error: {1}", this, ex); } } var sb = new StringBuilder(); sb.AppendFormat("<{0}>", options.RootElementName); foreach (var property in properties) { var value = XmlPropertyFormatter.Simplify(property.Value, options); if (options.OmitElementIfEmpty && string.IsNullOrEmpty(value)) { continue; } if (options.UsePropertyKeyAsElementName) { sb.AppendFormat("<{0}>{1}</{0}>", XmlPropertyFormatter.GetValidElementName(property.Key), value); } else { sb.AppendFormat("<{0} key='{1}'>{2}</{0}>", options.PropertyElementName, property.Key, value); } } sb.AppendFormat("</{0}>", options.RootElementName); return(sb.ToString()); }