Maybe, you need some logging for
your asp application. You want to log current time and some other values
to the log file and you need to have daily logs. This page contains some
small info about performance and locking problems in
ASP. |
1. The first solution you can
use is Scripting.FileSystemObject :
<%
....
DoLogFS1 "Some log value"
Function DoLogFS1(LogLine)
Dim OutStream, FileName
Const LogSeparator = " "
FileName = "D:\LogFiles\fs1" & Right("0" & _
Year(Now), 2) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & ".LOG"
Set OutStream = CreateObject("Scripting.FileSystemObject").OpenTextFile _
(FileName, 8, True)
OutStream.WriteLine Now() & LogSeparator & LogLine
End Function
%>
Simple function. But it takes most
of the time to create filesystem
object. 2. You can
put the FileSystemObject to global.asa and modify your
function: |
<Object RunAt=Server ID=FS ProgID=Scripting.FileSystemObject Scope=Application></Object>
and use the FileSystemObject i your ASP page:
<%
....
DoLogFS2 "Some log value"
Function DoLogFS2(LogLine)
Dim OutStream, FileName
Const LogSeparator = " "
FileName = "D:\LogFiles\fs2" & _
Right("0" & Year(Now), 2) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & ".LOG"
Set OutStream = FS.OpenTextFile (FileName, 8, True)
OutStream.WriteLine Now() & LogSeparator & LogLine
End Function
%>
This function takes 4 times less
time than version 1. But this function takes most of the time to open log
file for append.
3. We can do last
optimalization - store the file stream in application
: |
<Object RunAt=Server ID=FS ProgID=Scripting.FileSystemObject Scope=Application></Object>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Dim OutStream, FileName
FileName = "D:\LogFiles\fs2" & _
Right("0" & Year(Now), 2) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & ".LOG"
Set OutStream = FS.OpenTextFile (FileName, 8, True)
Set Application("OutStream") = OutStream
End Sub
</SCRIPT>
and use the stream i an ASP page:
<%
....
DoLogFS3 "Some log value"
Function DoLogFS3(LogLine)
Const LogSeparator = " "
Application("OutStream").WriteLine Now() & LogSeparator & LogLine
End Function
%>
The codes number 1. and 2. have
also also another problem - simultaneous requests. When the page is used
by many browsers, open of the log file for append may fail. For example,
if you do 10 000 requests from 10 browsers to the page, you wil have 9542
records in your log file (or similar number). Some log records will be
lost because first call of OpenTextFile for append locks the log file and
second call from another request will fail.
The code number 3 solves the
problem, but you have another problem - now you have no daily logs, but
only one log with date of start of ASP application.
4. We can
compare performance of a best solution for the problem, LogFile class. The class solves problems
with daily file name, simultaneous requests and many other problems and
tasks. Global.asa: |
<Object RunAt=Server ID=LogFile ProgID=ScriptUtils.LogFile Scope=Application></Object>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
LogFile.TemplateFileName = "D:\Log\LG%y%m%d.LOG"
End Sub
</SCRIPT>
ASP page:
<%
....
LogFile.Log "Some log value"
%>
Next table contains a short
performance comparison of the four source codes. The times was from PII
Celeron/500MHz. |
Code number
| Microsecond |
1. Simple FileSystemObject |
3 800 |
2. FileSystemObject in Application |
950 |
3. TextStream in Application |
150 |
4. LogFile class |
60 |
5. But you want to log more
values than only one.
Dim vCurr, vDbl, vStr, vDate, vEmpty
vCurr = CCur (256.235)
vDbl = CDbl (23215.132154)
vStr = "Some string value"
vDate = Now
We can use FileSystemObject and
code no. 3 or
LogFile class:
DoLogFS4 vStr & LogSeparator & vCurr & LogSeparator & _
vDbl & LogSeparator & vDate & LogSeparator & vEmpty
or
LogFile.Log vStr, vCurr, vDbl, vDate, vEmpty
Code
| Microsecond |
FileSystemObject |
230 |
LogFile class |
115 |
You can see that LogFile class has at least 2 times better
performance than text Stream from FileSystemObject. And the class
solves many other problems and work with logging
... |