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.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
August 06, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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 codeEnd Function Write to a Text file 

Logging To A Text File

 Function AddToLog(sOn Error Resume Next Err.Clear dim objFilefso2 objFile "c:TrackingLog.txt" set fso2 CreateObject("Scripting.FileSystemObject") If fso2.FileExists(objFileThen Set objFile fso2.OpenTextFile(objFile8objFile.WriteLine s Else Set objFile fso2.CreateTextFile(objFileTrueobjFile.WriteLine s End If objFile.close set fso2 Nothing If err.number <> 0 Then dim snumsdesc snum err.number sdesc err.description ErrorHandler snumsdesc End If End Function 

Error Handler

 Sub ErrorHandler(byVal errorNumbyVal errorDesctheDesc "Error Number: " errornum " Error Description: " errorDesc WshShell.LogEvent 1theDesc dim objFilefso2 objFile "c:TrackingLog.txt" set fso2 CreateObject("Scripting.FileSystemObject") If fso2.FileExists(objFileThen Set objFile fso2.OpenTextFile(objFile8objFile.WriteLine " -Error Number:" errorNum " Error Desc:" errorDesc Else Set objFile fso2.CreateTextFile(objFileTrueobjFile.WriteLine " -Error Number:" errorNum " Error Desc:" errorDesc End If objFile.close set fso2 Nothing Err.clear End Sub 

blog comments powered by Disqus
ASP ARTICLES

- Using MySQL with ASP
- ADO for the Beginner
- ADO.NET 101: Data Rendering with a DataGrid ...
- Introducing SoftArtisans OfficeWriter 3.0 En...
- Getting Remote Files With ASP
- The Real Basics of Functions in ASP
- Enhancing Readability with ASP
- Mimicking PHP's String Formatting Functions
- Windows Server Hacks 12, 77, and 98
- How to Sort a Multi-Dimensional Array
- Developing an Information Management Tool wi...
- What are Active Server Pages?
- Getting Remote Pages with ASP
- FTP’ing Files with ASP
- Apply Single-Sign-On to Your Application

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials