HomeASP Useful Error Handling And Logging In Scrip...
Useful Error Handling And Logging In Script
Here is a couple of functions that are useful when debugging using Windows Scripting Host files. Notice in every function, ON ERROR RESUME NEXT and ERR.CLEAR is called. When using WSH or any script language, the rich debugging environment isn't available. Being able to debug your scripts and continue to the completion is a real challenge.
This technique has been useful in several hundred line script files that I've done. Notice in Function SAMPLE, 1 - this will log to a file on the c:\TrackingLog.txt, 2 and if there are any errors inside the function, all errors will Log right after what function was called also to the c:\trackingLog.txt. You can add whatever functionality you like to the Error Handler. This has been useful to me anyway! Any questions give me a shout!
Put these as global functions at the top of your windows scripting host
Set WshShell
= WScript.CreateObject("WScript.Shell") Set WshNetwork = WScript.CreateObject("Wscript.Network") Set FSO = CreateObject("Scripting.FileSystemObject") Function Sample() On Error Resume Next Err.Clear AddToLog('-Starting the Sample Function') ....rest of the code. End Function Write to a Text file
Function AddToLog(s) On Error Resume Next Err.Clear dim objFile, fso2 objFile = "c:TrackingLog.txt" set fso2 = CreateObject("Scripting.FileSystemObject") If fso2.FileExists(objFile) Then Set objFile = fso2.OpenTextFile(objFile, 8) objFile.WriteLine s Else Set objFile = fso2.CreateTextFile(objFile, True) objFile.WriteLine s End If objFile.close set fso2 = Nothing If err.number <> 0 Then dim snum, sdesc snum = err.number sdesc = err.description ErrorHandler snum, sdesc End If End Function