Example:
In this article, we have a donated XML generator from Zumwalt that allows you to save any errors that occur during the running of your application to an external XML file that can be sent to the creator of the application (yourself) for review.
Steps to add this module to your projects (for VB.Net 2003), are very simple:
1) Right Click your project, then click on Add Module
2) Name the module "modShared" (your filename should show modShared.vb)
3) Paste the code in this article to your module.
4) Anywhere that you want to log the error in your application, simpley pass in as follows:
Try
Catch ex As Exception
LogError("MyClass.MyMethod()", Now.ToString, ex.ToString)
End Try
The MyClass is either the module or class that the error happened in, and the MyMethod is the calling function or sub that the error happed on.
Now, when someone states they are having a problem, they can zip up the Log folder that will exist in the game folder, that will contain a file called ErrorLog.XML
Option Explicit On
Imports System.Xml
Imports System.IO
Module modShared
Friend gWriter As XmlTextWriter
Friend gWriterDoc As XmlDocument
Public Sub LogError(ByVal inFunct As String, ByVal inTime As String, ByVal inEvent As String)
Dim root As XmlNode
'// Check to see if the directory exists
If Directory.Exists("Logs") Then
'// check for the ErrorLog file
If File.Exists("Logs\ErrorLog.xml") Then
'// append the log data to the existing output log file.
Try
gWriterDoc = New XmlDocument
gWriterDoc.Load("Logs\ErrorLog.xml")
root = gWriterDoc.DocumentElement
'// create element nodes
Dim nError As XmlNode = gWriterDoc.CreateElement("Events")
Dim nFunction As XmlNode = gWriterDoc.CreateElement("Function")
Dim nTime As XmlNode = gWriterDoc.CreateElement("Time")
Dim nEvent As XmlNode = gWriterDoc.CreateElement("Event")
'// create text nodes
Dim tFunction As XmlNode = gWriterDoc.CreateTextNode(inFunct)
Dim tTime As XmlNode = gWriterDoc.CreateTextNode(inTime)
Dim tEvent As XmlNode = gWriterDoc.CreateTextNode(inEvent)
'// attach text nodes to element nodes
nFunction.AppendChild(tFunction)
nTime.AppendChild(tTime)
nEvent.AppendChild(tEvent)
'// attach element nodes to error node
nError.AppendChild(nFunction)
nError.AppendChild(nTime)
nError.AppendChild(nEvent)
'// attach Error node to document node
root.AppendChild(nError)
'// save modified document
gWriterDoc.Save("Logs\ErrorLog.xml")
Catch ex As System.Exception
Finally
'// close the system.xml.xmltextwriter
If Not gWriter Is Nothing Then
gWriter.Close()
Application.DoEvents()
End If
End Try
Else
'// our log file doesn't exist but our folder is ready, lets create the output log file
Try
gWriter = New XmlTextWriter("Logs\ErrorLog.xml", System.Text.Encoding.Unicode)
gWriter.Formatting = Formatting.Indented
gWriter.WriteStartDocument()
gWriter.WriteStartElement("Logs")
gWriter.WriteStartElement("Events")
gWriter.WriteElementString("Function", inFunct)
gWriter.WriteElementString("Time", inTime)
gWriter.WriteElementString("Event", inEvent)
gWriter.WriteEndElement()
gWriter.WriteEndElement()
Catch ex As System.Exception
Finally
If Not gWriter Is Nothing Then
gWriter.Close()
Application.DoEvents()
End If
End Try
End If
Else
'// our directory doesn't exist, create it and then create the log
Try
Directory.CreateDirectory("Logs")
gWriter = New XmlTextWriter("Logs\ErrorLog.xml", System.Text.Encoding.Unicode)
gWriter.Formatting = Formatting.Indented
gWriter.WriteStartDocument()
gWriter.WriteStartElement("Logs")
gWriter.WriteStartElement("Events")
gWriter.WriteElementString("Function", inFunct)
gWriter.WriteElementString("Time", inTime)
gWriter.WriteElementString("Event", inEvent)
gWriter.WriteEndElement()
gWriter.WriteEndElement()
Catch ex As System.Exception
Finally
If Not gWriter Is Nothing Then
gWriter.Close()
Application.DoEvents()
End If
End Try
End If
End Sub
End Module