Write() публичный Метод

Writes the specified logEvent to XML
public Write ( XmlWriter writer, LogEvent logEvent ) : void
writer System.Xml.XmlWriter The to write to
logEvent NLog.Model.LogEvent The log event to write
Результат void
Пример #1
0
        public void ErrorWrite()
        {
            LogEvent logEvent = new LogEvent();

            string path = "blah.txt";
            try
            {
                string text = File.ReadAllText(path);
            }
            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();
        }
Пример #2
0
        /// <summary>Saves this instance the specified <paramref name="writer"/>.</summary>
        /// <param name="writer">The <see cref="XmlWriter"/> to save this instance to.</param>
        public void Save(XmlWriter writer)
        {
            var logWriter = new LogEventWriter();
            logWriter.Write(writer, this);

            writer.Flush();
        }