ASP Write Log processing method

Source: Internet
Author: User
Tags date log return servervariables syslog trim xsl xsl file
One, the realization method
A function that writes the log, provides several parameters, the user program calls this function can implement log record. Log to the XML file, the log file is generated by date, a new log file is created every day, the file name is: Yyyy_mm_dd.xml, respectively, the day of the month. And the View log is also daily simple, users want to see which day of the log, as long as the direct call to the XML file. Because an XML file already defaults to an XSL file to format the output.
Ii. Methods of writing journals
' Log-logging program
' Author: the Northern Snow
' Date: 2004.11.20
' Username: User information (identifies the person who performed the action)
' Operate: operation (indicating what the user has done)
' Userip: User IP (the IP address of the computer that identifies the user used to log on to the system)
' Opdate: Date the user action occurred
' Log writes an XML file, which is created the first time it is written, if the XML file does not exist.
' Return value: 1 An error occurred while opening the log file
' Return value: 9 to finish writing log file correctly


function Writesyslog (sys_userid,sys_username,operate)
Dim op_username
If trim (sys_userid) = "" and trim (sys_username) = "" Then
Op_username= "Anonymous"
Else
Op_username = Sys_userid & "/" & Sys_username
End If

Xmlpath= "/" & Getroot () & "/log/syslog/"
Xmlfile=replace (CStr (convertdate (date)), "-", "_") & ". Xml"
Rootnode= "syslog" log file root node name
Logfile=server.mappath (Xmlpath & xmlFile) ' Log file path
Set Fso=server. CreateObject ("Scripting.FileSystemObject")

' If the log file does not exist, create one and write header and root information
If not FSO. FileExists (LogFile) Then
Fso. CreateTextFile LogFile
Set FFF=FSO. GetFile (LogFile)
Set Mmm=fff.openastextstream (2)
Mmm.write "<?xml version=" "1.0" "encoding=" "gb2312" "?>" & vbCrLf & "<?xml-stylesheet type= ' text/xsl '" Href= '.. /loginfo.xsl '?> ' & vbCrLf & "<" & RootNode & "></" & RootNode & ">"
Set mmm=nothing
Set fff=nothing
End If
Set fso=nothing

Set xd = Server.CreateObject ("Msxml2.domdocument")
Xd.async = False
Xd.load (LogFile)
If Xd.parseerror.errorcode<>0 Then
Writesyslog=1 ' Open log file error
Exit function
End If

' Create new node information

Set Et=xd.documentelement



Set Cnode=xd.createelement ("Log")
Et.appendchild (Cnode)

Set Node2=xd.createelement ("username")
Node2.text=op_username
Cnode.appendchild (Node2)
Set Node2=xd.createelement ("Operate")
Node2.text=operate
Cnode.appendchild (Node2)
Set Node2=xd.createelement ("Userip")
Node2.text=request.servervariables ("REMOTE_ADDR")
Cnode.appendchild (Node2)
Set Node2=xd.createelement ("Opdate")
Node2.text=cstr (now ())
Cnode.appendchild (Node2)
Xd.save LogFile ' Write to log file

Set cnode=nothing
Set node2=nothing
Set xd=nothing
Writesyslog=9 ' Description writes the log information normally
End Function

' Get the name of the current virtual directory
function Getroot ()
Url=request.servervariables ("URL")
Url=right (Url,len (URL)-1)
getroot= Mid (Url,1,instr (URL, "/")-1)
End Function

' Put one digit in front of 0
function Fillzero (str)
Ttt=str
If Len (str) =1 Then
ttt= "0" & Str
End If
Fillzero=ttt
End Function
' Conversion date, will be a complement to zero 2003-1-2--> 2003-01-02
function Convertdate (tdate)
Ttt=tdate
If ISD



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.