Link to home
Start Free TrialLog in
Avatar of chriscboy
chriscboyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with writing error information to an XML file

Hi,

I am new to VB.NET/XML so please bear with me. I am trying to write a method called ErrorMethod() which will take an exception object and write the error information to an XML file. If the file already exists the error data should be appended to the XML file. This is what the XML file SHOULD look like:
<?xml version="1.0" ?>
<!-- Comments: WebServices Error Log  -->
<Errors>
      <Error>
      <Date>30/05/2006</Date>
      <Time>02:50:48</Time>
      </Error>
      <Error>
      <Date> 31/05/2006</Date>
      <Time> 02:50:48</Time>
      </Error>
      <Error>
      <Date>31/05/2006 </Date>
      <Time>12:50:48 </Time>
      </Error>
</Errors>

Each Error node contains the information about each error. Now this is what I am getting when I run the code and look at the XML file in notepad:

<?xml version="1.0"?>
<!--Comments: WebServices Error Log-->
<Errors>
&lt;Error&gt;
      &lt;Date&gt;
      30/05/2006
      &lt;/Date&gt;
      &lt;Time&gt;
      05:27:38
      &lt;/Time&gt;
&lt;/Error&gt;</Errors>

Could some please look at the code below and suggest what improvements could be made to make it look like it should. Thanks!!

Chris

---------------------------------------
And now for the code:

    Private Sub ErrorMethod(ByVal ex As Exception)
        ' Do something here to write error message to error log        
        ' Write Out : Date,Time,User,WebServiceName,MethodName,LineNo,ErrNo,Message
        Dim sErrFile As String = ConfigurationManager.AppSettings("ErrorLogFileName").ToString
        Dim sError As String
        Dim dToday As DateTime = System.DateTime.Now        

        sError = ""
        ' File doesn't exist so create it
        If Not File.Exists(sErrFile) Then
            Dim xmlWriter As New XmlTextWriter(sErrFile, Nothing)
            With xmlWriter
                .WriteStartDocument()
                .WriteComment("Comments: WebServices Error Log")
                .WriteStartElement("Errors")
                .WriteEndElement()
                .WriteEndDocument()
                .Close()
            End With
        End If

        ' Append Error node to end of Errors node
        Dim doc As New XmlDocument()
        Dim node As XmlDocumentFragment        
        Dim sXML As String = ""

        'build document...        
        sXML = String.Concat(sXML, ControlChars.CrLf, "<Error>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "<Date>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, dToday.ToString("dd/MM/yyyy"))
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "</Date>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "<Time>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, dToday.ToString("hh:mm:ss"))
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "</Time>")        
        sXML = String.Concat(sXML, ControlChars.CrLf, "</Error>")

        doc.Load(sErrFile)                              ' Load existing error log
        node = doc.CreateDocumentFragment()             ' Create document fragment from original XML doc
        node.InnerText = sXML                           ' Associate XML string with document fragment

        doc.DocumentElement.AppendChild(node)           ' Append document fragment to main document        
        doc.Save(sErrFile)                              ' Save XML document back to file
    End Sub
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) XML suggestion:

<Errors>
     <Error time="30/05/2006 02:50:48" />
     <Error time="31/05/2006 02:50:48" />
     <Error time="31/05/2006 12:50:48" />
</Errors>

  This will keep the file structure more compact and readable.

2) Are you going to limit the number of error written, or can the file size just keep growing to MB in size?

3) Plain text strings get HTML encoded, which is why '<' is written as &lt.

Bob
Avatar of chriscboy

ASKER

1) That might not work in my case. I do intend on adding additional elements to the error node. I am just trying to initially get it working with Date and Time elements but would also like to add elements for ErrorNumber,ErrorDescription,ErrorMethod etc.

2) No, this file will grow and grow (wouldn't it be nice if I got no errors!?!?)

3) Ah, that makes some sense, because that is what I saw when I opened the XML file in notepad. If I open in IE6, I get the following :
<?xml version="1.0" ?>
<!-- Comments: WebServices Error Log-->
 <Errors><Error><Date>30/05/2006</Date><Time>05:27:38</Time></Error></Errors>

The entire <Error>...</Error> appears in black as well.

Which would indicate I am doing something wrong in my code?
If you write CDATA elements, they don't get parsed/encoded.

Here is an example:

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
   {
   return 1
   }
else
   {
   return 0
   }
}
]]>
</script>

Bob
Sorry that did not make any sense! I'm completely new to XML so don't even know what CDATA means!

I am trying to write out a simple XML file with the tags previously mentioned. I think I will write the error info out to a simple CSV file, as I know what to do there. XML can wait until I understand it a bit more. Thanks for trying.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial