?
Solved

Help with writing error information to an XML file

Posted on 2006-05-30
6
Medium Priority
?
178 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:chriscboy
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16791958
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
0
 

Author Comment

by:chriscboy
ID: 16800241
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16800754
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
0
 

Author Comment

by:chriscboy
ID: 16805531
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16805936
Here is a very good XML tutorial:

http://www.w3schools.com/xml/default.asp

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Integration Management Part 2
Screencast - Getting to Know the Pipeline

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question