public void Write(LogMessage logMessage) { if (logMessage == null) { return; } lock (this.fileList) { FileLogConfig config = logMessage.LogActor.Config as FileLogConfig; // write xml information head if (!this.fileList.Contains(config.FileName)) { StreamWriter sw = new StreamWriter(config.FileName, false); this.fileList.Add(config.FileName); sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); sw.WriteLine("<TestLog " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://schemas.microsoft.com/windows/ProtocolsTest/2007/07/TestLog " + "http://schemas.microsoft.com/windows/ProtocolsTest/2007/07/TestLog.xsd\" " + "xmlns=\"http://schemas.microsoft.com/windows/ProtocolsTest/2007/07/TestLog\">"); sw.WriteLine("<LogEntries>"); sw.Close(); } // write log information if (config != null) { StreamWriter sw = new StreamWriter(config.FileName, true); sw.WriteLine("<LogEntry kind=\"{0}\" timeStamp=\"{1}\" module=\"{2}\" category=\"{3}\">", logMessage.LogLevel, string.Format(CultureInfo.InvariantCulture, "{0}.{1}Z", logMessage.Date.ToString("s", CultureInfo.InvariantCulture), logMessage.Date.Millisecond), logMessage.LogActor.Module, logMessage.LogActor.Category); sw.WriteLine("<Message>{0}</Message>", logMessage.Message); sw.WriteLine("</LogEntry>"); sw.Close(); } } }
private void ReadFileLogConfig() { System.Xml.XmlNode file = this.xmlNode.SelectSingleNode("//File"); if (file != null) { System.Xml.XmlNodeList configs = file.SelectNodes("Config"); foreach (System.Xml.XmlNode config in configs) { //id="stack" category="" level="DEBUG" module="stack" fileName="stack.log" System.Xml.XmlAttribute id = config.Attributes["id"]; System.Xml.XmlAttribute category = config.Attributes["category"]; System.Xml.XmlAttribute level = config.Attributes["level"]; System.Xml.XmlAttribute module = config.Attributes["module"]; System.Xml.XmlAttribute fileName = config.Attributes["fileName"]; if (id == null || fileName == null) { throw new StackException("config error: file config should have id and fileName."); } // create a config instance FileLogConfig fileLogConfig = new FileLogConfig(); fileLogConfig.Category = category.Value; fileLogConfig.Level = (LogLevel)Enum.Parse(typeof(LogLevel), level.Value); fileLogConfig.Module = module.Value; fileLogConfig.FileName = fileName.Value; if (this.logConfigs.ContainsKey(id.Value)) { throw new StackException("the id of config must be unique."); } this.logConfigs.Add(id.Value, fileLogConfig); } } }