Article Options

Structured Exception Handling in .NET (ApplicationException)
by Arnaldo Sandoval | Published  11/04/2006 | .NET Framework .NET Intermediate Windows Development | Rating:
Page 6

Private SaveToFile() method:
This is the method writing the exception details to a flat file that developers provide when calling the Save() method or to the default file coded in the class.

 Private Sub SaveToFile(ByVal TargetFile As String)         If TargetFile.Length = 0 Then             TargetFile = "C:\Temp\ApplicationErrors.log"         Else             If TargetFile.IndexOf(":\") < 0 Then                 TargetFile = "C:\Temp\" & TargetFile             End If         End If         Dim OutputFile As New System.IO.StreamWriter(TargetFile, True)         Dim sOutput As String         sOutput = Me.ErrorDate & "/"         sOutput &= Me.ErrorTime & "/"         sOutput &= Me.MachineName & "/"         sOutput &= Me.OSVersion & "/"         sOutput &= Me.UserName & "/"         sOutput &= Me.Source & "/"         sOutput &= Me.ApplicationVersion & "/"         sOutput &= Me.ApplicationCulture & "/"         sOutput &= Me.Method & "/"         sOutput &= Me.MethodAction & "/"         sOutput &= Me.Message & "/"         sOutput &= Me.StackTrace.Trim         OutputFile.WriteLine(sOutput)         OutputFile.Close()

TargetFile: This parameter contains the name and location of the file where the exception details will be written.

The first thing the method does is making sure a file name is provided, if the parameter is empty, it assigns the default target flat file: C:\Temp\ApplicationErrors.log, otherwise, it checks the given file name for a drive name sign within the name, searching for ":\" does the trick, if no evidence of a drive name is found, the code insert the folder "C:\Temp\".

WARNING: This approach has its weakness, and you may want to improve it.

1. The log file could be at each user workstation's C:\Temp folder.
2. Users' workstations C: drive could be hidden.
3. The drive validation seems poorly implemented, what about a network drive name?

The method keep going by opening the TargetFile, building a long string with all the exception details, separating them with pipes, appending the long string to the file and finally closing the TargetFile.

Private SaveToAccess() Method:
This method write our application's exception details to an Access Database, the access database is accessed thru a connection string, so as long as it is reachable we have nothing to worry about.

ConnectionStringKey: This parameter pass the connection string "key" as defined at the application's config file, for the database we are using. The sample application attached (Test_ErrorHandlingException) defined it as shown:



Because, it is a connection string, you should replace it with the appropriated entry for your database location; the connection string key name is: DBConnStringErrorHandlingException, and the class uses it as a default when an empty connection string key is passed.

The connection string uses the access database TargetErrorLog.mdb located at the C:\temp folder, the database name could be anything, and there are no restrictions, the same goes with the folder C:\temp.

As we are retrieving the connection string from the application's config file, we should include the namespace: Imports System.Configuration.ConfigurationSettings at the top of the class file, as previously explained.

WARNING: Keep in mind the SaveToAccess method lacks an strong validation for a missing connection string, you should make sure the application's config file has its entry before playing around with this class.