/// <summary> /// Appends the specified builder. /// </summary> /// <param name="builder">The builder.</param> /// <param name="logEventInfo">The log event info.</param> protected override void Append(StringBuilder builder, LogEventInfo logEventInfo) { var logEvent = new LogEvent(); logEvent.Populate(logEventInfo); string xml = logEvent.Save(); builder.Append(xml); }
private LogEvent ReadLog(XmlReader reader) { var logEvent = new LogEvent(); if (reader.IsEmptyElement) { reader.Skip(); return logEvent; } reader.ReadStartElement(); reader.MoveToContent(); while (reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None) { if (reader.NodeType != XmlNodeType.Element) { reader.MoveToContent(); continue; } string name = reader.LocalName; if (name == "SequenceID") logEvent.SequenceID = XmlConvert.ToInt32(reader.ReadElementString()); else if (name == "TimeStamp") logEvent.TimeStamp = XmlConvert.ToDateTime(reader.ReadElementString(), XmlDateTimeSerializationMode.RoundtripKind); else if (name == "Level") logEvent.Level = reader.ReadElementString(); else if (name == "LoggerName") logEvent.LoggerName = reader.ReadElementString(); else if (name == "Message") logEvent.Message = reader.ReadElementString(); else if (name == "StackTrace") logEvent.StackTrace = reader.ReadElementString(); else if (name == "Error") logEvent.Error = ReadError(reader); else if (name == "Properties") logEvent.Properties = ReadProperties(reader); else reader.Skip(); // skip unknown reader.MoveToContent(); } reader.ReadEndElement(); return logEvent; }
public void NestedErrorWrite() { LogEvent logEvent = new LogEvent(); string path = "blah.txt"; try { try { string text = File.ReadAllText(path); } catch (Exception ioex) { throw new ApplicationException("Error reading file.", ioex); } } catch (Exception ex) { var log = _logger.Error() .Message("Error reading file '{0}'.", path) .Exception(ex) .Property("Test", "ErrorWrite") .LogEventInfo; logEvent.Populate(log); } string fileName = string.Format("LogEvent-{0}.xml", DateTime.Now.Ticks); string xml = logEvent.Save(); File.WriteAllText(fileName, xml); string outputFileName = Path.ChangeExtension(fileName, ".Writer.xml"); var settings = new XmlWriterSettings { Indent = true }; var writer = XmlWriter.Create(outputFileName, settings); var eventWriter = new LogEventWriter(); eventWriter.Write(writer, logEvent); writer.Flush(); writer.Close(); string newXml = File.ReadAllText(outputFileName); var newEvent = LogEvent.Load(newXml); newEvent.Should().NotBeNull(); }
public void Write(XmlWriter writer, LogEvent logEvent) { writer.WriteStartElement("LogEvent"); if (logEvent.SequenceID != default(int)) writer.WriteElementString("SequenceID", XmlConvert.ToString(logEvent.SequenceID)); if (logEvent.TimeStamp != default(DateTime)) writer.WriteElementString("TimeStamp", XmlConvert.ToString(logEvent.TimeStamp, XmlDateTimeSerializationMode.RoundtripKind)); if (logEvent.Level != null) writer.WriteElementString("Level", logEvent.Level); if (logEvent.LoggerName != null) writer.WriteElementString("LoggerName", logEvent.LoggerName); if (logEvent.Message != null) writer.WriteElementString("Message", logEvent.Message); if (logEvent.StackTrace != null) writer.WriteElementString("StackTrace", logEvent.StackTrace); if (logEvent.Error != null) { writer.WriteStartElement("Error"); WriteError(writer, logEvent.Error); writer.WriteEndElement(); // Error } if (logEvent.Properties != null) { writer.WriteStartElement("Properties"); WriteProperties(writer, logEvent.Properties); writer.WriteEndElement(); // Properties } writer.WriteEndElement(); // LogEvent }