Create and Write Log File

The subprocedure can be called by any ASP applicaiton/page which needs to create a log entry each time it is accessed. This can then be checked at a later stage by an administrator for example, to determine who/how/when details concerning a given application/ASP page. As it stands at the moment the log subprocedure records the data&time, client IP address, and path of the application/page which called the function to each entry made in the log file.

CodeFunctionName
What is this?

Public

Not Tested

Original Work
<%
Sub WriteToLog(strUserLogFileName)
'Pav Gill 18/6/1
'If you have found this useful and think
' you want to give me a job e-mail me:
'pav@docgill.freeserve.co.uk - South UK
' preferred. Cheers!
'WriteToLog is a simple asp subprocedure
' .
'Allows log entry (details of the log en
' try can be altered/user defined see belo
' w) to be written to
'a log file(user defined name passed by
' parameter string to the subprocedure) ea
' ch time the function is called
'from an .asp page. The .asp should incl
' ude this function via an '#include file=
' "WriteToLog.asp"
'statement at the top of the .asp script
' file.
'The path of the log file is that of the
' parent page where the function has been
' included.
'The user must pass in a parameter, strU
' serLogFileName, which is then converted
' to
'strUserLogFileName.log. Thus this becom
' es the file name of the log file saved i
' n the parent
'folder of the WriteToLogFunc.asp functi
' on.
'NOTE: NOT ON BRINKSTER!!- DIRECTORY OF
' LOG FILE MUST BE DB FOR SECUIRITY REASON
' S
'IF YOU TEST THIS FUNCTION ON BRINKSTER,
' YOU MUST CHANGE THE CODE AS OUTLINED BEL
' OW
'ESSENTIALLY CHANGE THE PATH OF THE LOG
' FILE TO THE DB DIRECTORY
'A string parameter without a file exten
' tion should be passed to this function.
'As this file is intended to be an #incl
' ude file: The statements Option explicit
' and the
'@Language=VBScript have not be placed i
' n the header, this resloves problems ass
' ociated
'with misplacements & repetition of thes
' e statements once the file is included
'this can cause run time errors.
Const ForAppending=8
Dim strLogFileName, strFolderPath, strPhysicalPath,strLogEntryTime
Dim objFSO,objApplicationFolder,objApplicationFile,objLogFileFSO,objLogFileTransaction

Set objFSO=CreateObject("Scripting.FileSystemObject")

'First retrieve the parent directory path where the LogFile
'is kept

strPhysicalPath=Server.MapPath(Request.ServerVariables("PATH_INFO"))
Set objApplicationFile=objFSO.getFile(strPhysicalPath)
Set objApplicationFolder=objApplicationFile.ParentFolder
strFolderPath=objApplicationFolder.path
'create the String path To the log file within the parent directory
'of this application,
'NOTE: For BRINKSER YOU HAVE STORE THE FILE IN THE \db\ DIRECTORY
'So the code To generate path String will be: strLogFileName=strFolderPath+"\db\"+strLogFileName

strLogFileName=strUserLogFileName+".log"'Generate the correct filename With extension
strLogFileName=strFolderPath+"\"+strLogFileName 'Generate the full path String


'create/open log file FileSystemObject
Set objLogFileFSO=CreateObject("Scripting.FileSystemObject")
'test whether file exists To either write/append to file
if objLogFileFSO.FileExists(strLogFileName) Then
Set objLogFileTransaction=objLogFileFSO.OpenTextFile(strLogFileName,ForAppending)
'FOR BRINKSTER the above line should be:
' Set objLogFileTransaction = objFSO.OpenTextFile((Server.MapPath("/YOURUSERAREANAME/db/"+strLogFileName)),ForAppending)
Else
Set objLogFileTransaction=objLogFileFSO.CreateTextFile(strLogFileName)
'FOR BRINKSTER the above line should be:
'
'Set objLogFileTransaction = objFSO.Crea
' teTextFile((Server.MapPath("/YOURUSERARE
' ANAME/db/"+strLogFileName)))
End if
'THE FOLLOWING LINE DISPLAYS STORED LOG FILE NAME AND PATH, AND CAN BE COMMENTED OUT!
Response.Write " <BR >FROM WriteLogFunc: " & strLogFileName &" <BR >"

strLogEntryTime=NOW

'This is where the actual log data is written,
'alter anything below To alter the data to be entered into the log file
'chr(9) is a VB tab character, which makes the log file easier To read.
'At the moment it's simply the Date and time the .asp script was accessed, the IP address of
'the client, and the name of the file which generated the log entry To be written.
'The last entry allows a Single log file To be written to by various .asp scripts, and allow the origin
'of Each entry To be determined.
objLogFileTransaction.WriteLine strLogEntryTime & chr(9) &_
chr(9) & (Request.ServerVariables("HTTP_X_FORWARDED_FOR")) &_
chr(9) & strPhysicalPath

objLogFileTransaction.Close
'ensure that all memory is unallocated once log entry has been created
Set objLogFileTransaction=Nothing
Set objLogfileFSO=Nothing
Set objFSO=Nothing
Set objApplicationFile=Nothing
Set objApplicationFolder=Nothing
End Sub
% >

(strUserLogFileName)

Views 4,572

Downloads 1,475

CodeID
DB ID

PavGill
1
Revisions

v1.0

Saturday
April
14
2018