xenia27
asked on
Write XML Log
Hi,
I want to write a log in xml format...a simple log...just recording date and time and some context...
This is what I do...
WriteLog("Start Program...");
WriteLog("Testing 1...");
WriteLog("Testing 2...");
public void WriteLog(string logstr)
{
XmlTextWriter xmlWriter = new XmlTextWriter("HttpListene rLog.xml", System.Text.Encoding.UTF8) ;
DateTime dt = DateTime.Now;
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI nstruction ("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElemen t("MSG");
xmlWriter.WriteAttributeSt ring("Time ", dt.ToString());
xmlWriter.WriteAttributeSt ring("Msg" , logstr);
xmlWriter.Close();
}
Here is what I wanna see...
<?xml version='1.0' encoding='UTF-8'?>
<MSG>
<INFO Time="2006/11/1 AM 11:08:48" Msg="Start Program..." />
<INFO Time="2006/11/1 AM 11:08:50" Msg="Testing 1..."/>
</MSG>
Something like this. How can I modify my code to do this?
I want to write a log in xml format...a simple log...just recording date and time and some context...
This is what I do...
WriteLog("Start Program...");
WriteLog("Testing 1...");
WriteLog("Testing 2...");
public void WriteLog(string logstr)
{
XmlTextWriter xmlWriter = new XmlTextWriter("HttpListene
DateTime dt = DateTime.Now;
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI
xmlWriter.WriteStartElemen
xmlWriter.WriteAttributeSt
xmlWriter.WriteAttributeSt
xmlWriter.Close();
}
Here is what I wanna see...
<?xml version='1.0' encoding='UTF-8'?>
<MSG>
<INFO Time="2006/11/1 AM 11:08:48" Msg="Start Program..." />
<INFO Time="2006/11/1 AM 11:08:50" Msg="Testing 1..."/>
</MSG>
Something like this. How can I modify my code to do this?
you will need to do a foreach inside your function to get all of the INFO fields.
public void WriteLog(string logstr)
{
DateTime dt = DateTime.Now;
XmlTextWriter xmlWriter = new XmlTextWriter("HttpListene
//This is the start of the document
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI
xmlWriter.WriteStartElemen
****Do a foreach here
xmlWriter.WriteStartElemen
xmlWriter.WriteAttributeSt
xmlWriter.WriteAttributeSt
xmlWriter.WriteEndElement(
*** end foreach
xmlWriter.WriteEndElement(
//this is the end of the document
xmlWriter.WriteEndDocument
xmlWriter.Flush();
xmlWriter.Close();
}
ASKER
If I have something existed in HttpListenerLog.xml file and I would like to keep it in my file and add new context, how can I do so?
Thanks for your help~
Thanks for your help~
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help~ ^^
{
DateTime dt = DateTime.Now;
XmlTextWriter xmlWriter = new XmlTextWriter("HttpListene
//This is the start of the document
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI
xmlWriter.WriteStartElemen
xmlWriter.WriteStartElemen
xmlWriter.WriteAttributeSt
xmlWriter.WriteAttributeSt
xmlWriter.WriteEndElement(
xmlWriter.WriteEndElement(
//this is the end of the document
xmlWriter.WriteEndDocument
xmlWriter.Flush();
xmlWriter.Close();
}