Microsoft Silverlight – Error Logging to Isolated Storage

Isolated storage location is under

C:Users*username*AppDataLocalLowMicrosoftSilverlightis

To handle error logging in Silverlight, create a clsIsolatedStorage.vb

Imports System.IO
Imports System.Xml
Imports System.IO.IsolatedStorage
Imports MyApplication.clsApplicationGlobal
    Public Class clsIsolatedStorage
        Shared Sub ErrorHandler(ByVal mMode As String, ByVal Procedure As String, ByVal e As Exception)
            Try
                WriteToXML(mMode, Procedure, e)
            Catch ex As Exception
                ‘Big Problem
            End Try
        End Sub

        Shared Sub WriteToXML(ByVal mmode As String, ByVal mproc As String, ByVal e As Exception 
            Try
                Using store As IsolatedStorageFile = _
                IsolatedStorageFile.GetUserStoreForApplication()
                    ‘ Create a directory for the files
                    ‘ if it doesn’t already exist. subDirName As String = "SLErrors"
                    If Not store.DirectoryExists(subDirName) Then
                        store.CreateDirectory(subDirName)
                    End If
                End Using

                Dim isoStore As IsolatedStorageFile = _ 
                IsolatedStorageFile.GetUserStoreForApplication()
                ‘ Create/Open file
                Dim isoStream As IsolatedStorageFileStream = _
                New IsolatedStorageFileStream("" & subDirName & "" & "ErrorLog.txt", _
                FileMode.Append, FileAccess.Write, isoStore) "" FileMode.Append, FileAccess.Write
                ‘ Write to the Isolated Storage for the user.

                Using streamWriter As StreamWriter = New StreamWriter(isoStream 
                    Dim settings As XmlWriterSettings = New XmlWriterSettings()
                    settings.Indent = True
                    ‘ Create an XmlWriter.
                    Using writer As XmlWriter = XmlWriter.Create(streamWriter, settings)
                        writer.WriteStartElement("Error")
                        writer.WriteElementString("DateTime", Now)
                        writer.WriteElementString("Module", mmode)
                        writer.WriteElementString("Procedure", mproc)

                        If IsNothing(e) = False Then writer.WriteElementString("ExceptionMessage", e.Message 
                        writer.WriteEndElement()
                        writer.Flush()
                    End Using
                End Using
                isoStream.Close()
                ‘ Open the file again for reading.
                ‘Using reader As New StreamReader(isoStore.OpenFile("SLErrorLog.txt", FileMode.Open))
                ‘ HoldErrors = HoldErrors & reader.ReadToEnd()
                ‘End Using
            Catch ex As Exception
            End Try
        End Sub
    End Class

To use the class:

Private eMod As String = "PageName"
Private eProc As String = ""

Private Sub btnSameAddress_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs 
    eProc = "btnSameAddress_Click"
    Try
        txtAddress.Text = HoldReporterStreet
        txtApartment.Text = HoldReporterApt
        txtCity.Text = HoldReporterCity
        txtState.Text = HoldReporterState
        txtZip.Text = HoldReporterZip
		
    Catch ex As Exception
        clsIsolatedStorage.ErrorHandler(eMod, eProc, ex)
 
    End Try
    txtAddress.Focus()
End Sub

I have a clsApplicationGlobal that holds variables that I use from one page to the next. You may be able to put the public shared in the clsIsolatedStorage as a private variable.

MyApplication_Silverlight_Application.clsApplicationGlobal
 
Public Shared subDirName As String = "SLErrors"

Leave a Reply