PDA

View Full Version : server date and automatic log files??


kab_184
08-19-2003, 11:03 AM
hi

i got this code working fine

<%
dim iplg, logFile8
set iplg=createObject("scripting.FileSystemObject")
set logFile8=iplg.openTextFile(server.mappath("IPLOG.txt"),8)
logFile8.write Request.ServerVariables("Remote_Addr") & vbcrlf
logFile8.close
set logFile8=nothing
set iplg=nothing
%>

but

1- now i think this script doesnt do one things.. what if the "iplog.txt " file doesnot exist in the specified address? it would give errors?? what code lines shall i have to add to check that iplog.txt file is present and if not present it should automatically create the iplog.txt file.what lines should be added in the following

<%
dim iplg, logFile8
set iplg=createObject("scripting.FileSystemObject")
set logFile8=iplg.openTextFile(server.mappath("IPLOG.txt"),8)
logFile8.write Request.ServerVariables("Remote_Addr") & vbcrlf
logFile8.close
set logFile8=nothing
set iplg=nothing
%>




2-another thing i thought was what if 2 or more people were accessing the same file at the same time? wont it give error if iplog.txt file was already opened to log ip of first visitor while the second visitors was trying to write its ip in the same file... i mean one file "iplog.txt" file being accessed by 2 or more users at the same time.will iplog.txt file can write 2 more different ip at the same time?what code should be edited /added to achive this?

3- i want to create iplog.txt file separate for each day. For this i think server date can be used can it?

for example if the date on server is 07/22/2003 how can i make an iplog file wiht the name 07222003iplog.txt . i mean adding the server date as suffix or prefix to create txt file automatically .
so next day as the derver date changes the next iplog file is created as" 08222003iplog.txt" and all the ip addresses are written in that file on that day and date. That will surelymake my life easier as ip addresses shall be written as daily log files instead of one signle file automatically.... so can this be done in asp...what changes shall i have to make to the followiong code to achive this


<%
dim iplg, logFile8
set iplg=createObject("scripting.FileSystemObject")
set logFile8=iplg.openTextFile(server.mappath("IPLOG.txt"),8)
logFile8.write Request.ServerVariables("Remote_Addr") & vbcrlf
logFile8.close
set logFile8=nothing
set iplg=nothing
%>


please help me as this is more complex for me ..

thank you

raf
08-19-2003, 11:54 AM
1. You need to use the file.exists method http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject.html
if it returns false, then create the file

2. the file will be locked for the second user. But that's not a real problem, since the operation will be repeated after the file is released. (fraction of a second on your files, assuming they are quite small) That is why databases are better for multiple user trafic. There the locking is done at rowlevel, not file or tablelevel. And i you're appending, no lock is even needed. Only the key-value needs to be generated/allocated.

3. Just compose the filename and then create the file, like

dim filecheck, filesys, filetxt, filen
filen = Date & "iplog.txt"

Set filecheck = CreateObject("Scripting.FileSystemObject")
If filecheck.FileExists(filen)=False Then
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile(filen, True)
end if

then your code to append to the file

Roelf
08-19-2003, 11:55 AM
1: set logFile8=iplg.openTextFile(server.mappath("IPLOG.txt"),8,
True )

the true will create the file if it doesnt exist

2: before this writing to the file, lock the application and after the writing, unlock it

Application.Lock
dim iplg, logFile8
set iplg=createObject("scripting.FileSystemObject")
set logFile8=iplg.openTextFile(server.mappath("IPLOG.txt"),8, True)
logFile8.write Request.ServerVariables("Remote_Addr") & vbcrlf
logFile8.close
set logFile8=nothing
set iplg=nothing
Application.Unlock

3: do as you suggested, add the date into the filename
Application.Lock
dim iplg, logFile8, filename
filename = Day(Now) & Month(Now) & Year(Now) & "IPLOG.txt"
set iplg=createObject("scripting.FileSystemObject")
set logFile8=iplg.openTextFile(server.mappath(filename),8,
True)
logFile8.write Request.ServerVariables("Remote_Addr") & vbcrlf
logFile8.close
set logFile8=nothing
set iplg=nothing
Application.Unlock

glenngv
08-21-2003, 08:14 AM
Application.Lock (http://www.devguru.com/Technologies/asp/quickref/application_lock.html) is only used to prevent simultaneous changes in any variable in the Application object not in modifying files. I think IIS automatically handles simultaneous requests to edit a file by queuing the requests. I have a monthly log file in the my intranet app in my work. I have this code and never experienced any log file creation problems ever since I implemented it 2 years ago. Don't say no one visits my site, :D. Actually, I've got decent number of visitors (sessions) per day. :)


'global.asa
Sub Session_OnStart
dim mo, yr, logfile, filesys, filetxt
mo = DatePart("M", Date())
yr = DatePart("YYYY", Date())
if mo<10 then mo="0" & mo
logfile = mo & yr & ".txt"
Set filesys = CreateObject("Scripting.FileSystemObject")
'iomode: ForReading = 1, ForWriting = 2, ForAppending = 8
If filesys.FileExists(logfile) Then iomode=8 else iomode=2
Set filetxt = filesys.OpenTextFile(server.mappath(logfile), iomode, True)
filetxt.WriteLine(request.servervariables("REMOTE_ADDR") & " = " & Now())
filetxt.Close
set filetext = nothing
set filesys = nothing
End Sub

whammy
08-21-2003, 04:50 PM
Hmm, I was going to comment on all this but I think Roelf and glenngv covered it quite nicely.