ASP-Written log processing method (XML)

Source: Internet
Author: User
Tags date log syslog variable trim xmlns xsl xsl file
Xml

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 IsDate (tdate) Then
Ttt=year (tdate) & "-" & Fillzero (Month (tdate)) & "-" & Fillzero (Day (tdate))
End If
Convertdate=ttt
End Function

Third, user-formatted XSL files:

<?xml version= "1.0"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" urn: Schemas-microsoft-com:xslt "xmlns:user=" http://www.cccar.com.cn/"
exclude-result-prefixes= "msxsl User" >

<!--localized strings-->
<xsl:variable name= ' columnheader_username ' > User </xsl:variable>
<xsl:variable name= ' columnheader_time ' > Time </xsl:variable>
<xsl:variable name= ' columnheader_operate ' > Operation </xsl:variable>
<xsl:variable name= ' columnheader_address ' >ip address </xsl:variable>

<!--variables
<xsl:variable name= ' TableStyle ' >background-color: #DAE6D8; Font-family:simsun, Verdana; font-size:75%; Text-align:left; Vertical-align:top</xsl:variable>
<xsl:variable name= ' HeaderStyle ' >background:a0b0a8;color:# 000000;border-bottom:1 solid black;border-top:1 solid black</xsl:variable>
<msxsl:script language= " JavaScript "implements-prefix=" user >
   function xmldatetime (nodelist) {
            return Date.parse (Nodelist.replace (/-/g, "/"));
       }
 </msxsl:script>
<xsl:output omit-xml-declaration= "yes"/>
<xsl:template match= " Syslog "

<title>
Log view
</title>

<body style= ' margin:10;background-color: #DAE6D8 ' >
<div align= "center" >
<table style= "{$TableStyle}" width= "100%" align= "center" cellspacing= ' 0 ' >

<thead>
<tr height= ">"

<th width= "15%" style= "{$HeaderStyle}" >
<xsl:value-of select= "$ColumnHeader _username"/>
</th>
<th width= "20%" style= "{$HeaderStyle}" >
<xsl:value-of select= "$ColumnHeader _time"/>
</th>
<th width= "50%" style= "{$HeaderStyle}" >
<xsl:value-of select= "$ColumnHeader _operate"/>
</th>
<th width= "15%" style= "{$HeaderStyle}" >
<xsl:value-of select= "$ColumnHeader _address"/>
</th>
</tr>
</thead>

<tbody style= ' Vertical-align:top ' >
<tr ><td colspan= "4" height= "5" ></td></tr>
<xsl:for-each select= "Log" >
<xsl:sort order= ' ascending ' select= "User:xmldatetime (String (opdate))" data-type= "number"/>

<tr height= ">"

&LT;TD valign= "Bottom" ><xsl:value-of select= "username"/></td>
&LT;TD valign= "Bottom" ><xsl:value-of select= "Opdate"/></td>
&LT;TD valign= "Bottom" ><xsl:value-of select= "operate"/></td>
&LT;TD valign= "Bottom" ><xsl:value-of select= "Userip"/></td>
</tr>
<tr bgcolor= "#999999" ><td colspan= "4" height= "1" ></td></tr>
</xsl:for-each>

&LT;TR&GT;&LT;TD colspan= "4" align= "right" > Total: <xsl:value-of select= "count (log)"/> Bar </td></tr>
</tbody>
</table>
</div>
</body>

</xsl:template>
</xsl:stylesheet>



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.