You can choose to create a XML file in which detail records are represented as Elements or Attributes only. It is wiser to use the best use of both, more use of attribute reduces the size of xml file and increases the speed over wire. Representation of data in Elements only XML file becomes heavier in file size thus slower the transmission. The only benefit is when you open this file in browser it is relatively easily to read. Following are an example for creating a file with same information in two different ways.
XML Element Format
This format takes more file size, but increases the readability of the file in browser. This example can be used for gathering all the exceptions generated during application uses.
Private Sub LogErrorAsXMLFileElement(ByVal excep As Exception, ByVal serverName As String, _
Optional ByVal remoteHostAddress As String = "", Optional ByVal referer As String = "")
Dim xmlDataDoc As New XmlDataDocument
Dim xmlDeclare As XmlDeclaration = Nothing
Dim rootNode As XmlNode = Nothing
Dim childElement As XmlElement = Nothing
Dim parentElement As XmlElement = Nothing
Try
Dim rootTag As String = "Errors." & serverName.ToLower
Dim filePathName As String = WebConfigurationManager.AppSettings("ErrorLogFolder") & "\" & _
serverName & ".ErrorLog." & Format(Now(), "yyyyMM").ToString & "E.xml"
If File.Exists(filePathName) Then
xmlDataDoc.Load(filePathName)
rootNode = xmlDataDoc.GetElementById(rootTag)
Else
xmlDeclare = xmlDataDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)
rootNode = xmlDataDoc.CreateElement(rootTag)
xmlDataDoc.AppendChild(rootNode)
xmlDataDoc.InsertBefore(xmlDeclare, rootNode)
End If
childElement = xmlDataDoc.CreateElement("Error", "")
rootNode.AppendChild(childElement)
parentElement = childElement
childElement = xmlDataDoc.CreateElement("ID", "")
childElement.InnerText = XmlConvert.ToString(Guid.NewGuid)
parentElement.AppendChild(childElement)
childElement = xmlDataDoc.CreateElement("DateCreated", "")
childElement.InnerText = XmlConvert.ToString(Now, XmlDateTimeSerializationMode.Local)
parentElement.AppendChild(childElement)
childElement = xmlDataDoc.CreateElement("RemoteHostAddress", "")
childElement.InnerText = remoteHostAddress
parentElement.AppendChild(childElement)
childElement = xmlDataDoc.CreateElement("Referer", "")
childElement.InnerText = referer
parentElement.AppendChild(childElement)
childElement = xmlDataDoc.CreateElement("Message", "")
childElement.InnerText = excep.Message
parentElement.AppendChild(childElement)
childElement = xmlDataDoc.CreateElement("Exception", "")
childElement.InnerText = excep.ToString
parentElement.AppendChild(childElement)
xmlDataDoc.Save(filePathName)
Catch exc As Exception
Throw New Exception(exc.Message)
Finally
xmlDataDoc = Nothing
End Try
End Sub
XML Attribute Format
This format takes less file size, but decreases the readability of the file in browser. This example can be used for gathering all the exceptions generated during application uses.
Private Sub LogErrorAsXMLFileAttribute(ByVal excep As Exception, ByVal serverName As String, _
Optional ByVal remoteHostAddress As String = "", Optional ByVal referer As String = "")
Dim xmlDataDoc As New XmlDataDocument
Dim xmlDeclare As XmlDeclaration = Nothing
Dim rootNode As XmlNode = Nothing
Dim childElement As XmlElement = Nothing
Try
Dim rootTag As String = "Errors." & serverName.ToLower
Dim filePathName As String = WebConfigurationManager.AppSettings("ErrorLogFolder") & "\" & _
serverName & ".ErrorLog." & Format(Now(), "yyyyMM").ToString & "A.xml"
If File.Exists(filePathName) Then
xmlDataDoc.Load(filePathName)
rootNode = xmlDataDoc.SelectSingleNode(rootTag)
Else
xmlDeclare = xmlDataDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)
rootNode = xmlDataDoc.CreateElement(rootTag)
xmlDataDoc.AppendChild(rootNode)
xmlDataDoc.InsertBefore(xmlDeclare, rootNode)
End If
childElement = xmlDataDoc.CreateElement("Error")
With childElement
.SetAttribute("ID", XmlConvert.ToString(Guid.NewGuid))
.SetAttribute("DateCreated", XmlConvert.ToString(Now, XmlDateTimeSerializationMode.Local))
.SetAttribute("RemoteHostAddress", remoteHostAddress)
.SetAttribute("Referer", referer)
.SetAttribute("Message", excep.Message)
.SetAttribute("Exception", excep.ToString())
End With
rootNode.AppendChild(childElement)
xmlDataDoc.Save(filePathName)
Catch exc As Exception
Throw New Exception(exc.Message)
Finally
xmlDataDoc = Nothing
End Try
End Sub
No comments:
Post a Comment