Home > Microsoft .Net Development Tips > ARCHIVE: Tips & Tricks > Log errors to a file
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ARCHIVE: TIPS & TRICKS

Log errors to a file


Marlex Ventura
08.22.2000
Rating: -4.56- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Log errors to a file

Reader, Marlex L. Ventura of Manila, Philippines, submitted this week's tip. The following code encapsulates a Generic Error Handling routine that logs errors to a file. In most cases, developers would like to handle errors within their code. However a great deal of time is required to really eliminate all these possible sources of errors. Without an error handler routine, chances are your application may freeze or simply bomb out on unexpected situations. Much worse, users are not able to report back properly the conditions of the error and for convenience the developer would need to trace these errors to specific procedures or forms for a quick fix.

These lines of code are composed of 2 parts:
1. Error Handler used in procedure bodies.
2. Error Logger routine to log errors to an 'Error.log' file found in the same directory as your application.

The Error Handler can simply be copied and pasted on all of your existing procedures modifying the error messages, procedure names and other parameters as required.

Logically the Error Logger routine must be placed in a module available to all procedures and functions that would need to call the said routine (eg. modErrors.bas).

Next time your users encounter an error, simply ask them to e-mail the error log file and you will know where to start debugging your programs.


Private Sub Form_Load()
Dim strSomeCommand As String
Dim rs As New ADODB.Recordset
On Error GoTo errorproc
'A query string is used as a command string
strSomeCommand = "SELECT * FROM Employees WHERE EmpNo = '00001'"
Set rs = MyConn.Execute(strSomeCommand)

If Not rs.EOF Then
txtEmpName = rs!EmpName
txtAddr = rs!Addr
End If

rs.Close
Set rs = Nothing
Exit Sub 'exit point of a successful operation
'Error Handler
errorproc:
Select Case E

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ARCHIVE: Tips & Tricks
SearchVB wants your tips
Find out what VS.NET modules are installed
Retrieving icons from an EXE-file
Toggling Boolean values
VS.NET multiple tabbed windows
Set the value of a DataField in code behind
Easy grid design
Open using last Visual Studio.NET layout
Save development time by using inherited forms
Reusing code in VS.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


rr.Number
'handle known errors here
'Case 999
'Add procedure to fix error here then
'Resume

Case Else 'Catch All for other errors
'Report error based on the general purpose of the procedure
Select Case MsgBox("Error loading employee information. " & _
Trim(Err.Description), vbExclamation + vbRetryCancel +
vbDefaultButton1, "Load Employee Error")
Case vbRetry
Resume
Case vbCancel
'procedure that logs the error
Call LogError(Now, "Form_Load", Err, Me, strSomeCommand)
End Select
End Select
End Sub
'Error Logger
Public Sub LogError(ByVal ErrDateTime As Date, ByVal ProcName As
String, ByRef ErrObj As ErrObject, Optional FormObject As Object,
Optional CommandString As String)
Dim intFileO As Integer

On Error Resume Next

intFileO = FreeFile(0)

Open App.Path & "Error.log" For Append As intFileO

If IsMissing(FormObject) And IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source
ElseIf Not IsMissing(FormObject) And Not IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source, FormObject.Caption, CommandString
'To enhance this procedure you could add a routine to dump all values
found in a form such as:
'Call DumpFormValues(intFileO, FormObject)
ElseIf Not IsMissing(CommandString) Then
Write #intFileO, ErrDateTime, ProcName, ErrObj.Description,
ErrObj.Source, CommandString
End If

Close intFileO
End Sub

Marlex, thanks for the great tip. To show our appreciation, we're sending you a free searchVB.com denim shirt. Keep those tips coming in, folks!
-----------------------------------------
Marlex L. Ventura
ventura@usinc.net

Marlex currently works as the Director for Research & Development of Inobbar Technologies Corporation (http://www.inobbar.com), an Information and Communication Technology Consultancy Services company based in the Philippines.

His work in Research & Development has required Marlex to be constantly in tune with the different ways IT is being used in the industry, and constantly thinking of new ways of how IT can be leveraged to improve everyone's way of life.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts