ASP写的日志处理方法 (XML)

王朝网络·asp·作者佚名  2006-11-24
宽屏版  字体: |||超大  

一、实现方法

一个书写日志的函数,提供几个参数,用户程序调用这个函数就可以实现日志的记录。日志记录到xml文件中,日志文件按日期生成,每天新建立一个日志文件,文件名为:yyyy_mm_dd.xml,分别用了年月日。而查看日志也日常简单,用户想看哪天的日志,只要直接调用该xml文件即可。因为xml文件已经默认了一个xsl文件来格式化输出。

二、书写日志的方法

'记录日志的程序

'作者:塞北的雪

'日期:2004.11.20

'username :用户信息(标示进行该操作的人员)

'operate :操作(标示用户进行了什么操作)

'userip :用户IP(标示用户用于登录系统的计算机的IP地址)

'opdate :用户操作发生的日期

'日志写入一个xml文件,第一次写入时如果xml文件不存在,则创建。

'返回值:1 表示打开日志文件时出错

'返回值:9 表示正确完成写入日志文件

function WriteSysLog(sys_userid,sys_username,operate)

dim op_username

if trim(sys_userid)="" and trim(sys_username)="" then

op_username="匿名"

else

op_username = sys_userid & "/" & sys_username

end if

xmlPath="/" & getRoot() & "/log/SysLog/"

xmlFile=replace(cstr(ConvertDate(date())),"-","_") & ".xml"

RootNode="syslog" '日志文件根节点名字

LogFile=server.mappath(xmlPath & xmlFile) '日志文件路径

set fso=server.CreateObject("scripting.filesystemobject")

'如果日志文件不存在,就创建一个,并写入头信息和根信息

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 '打开日志文件出错

exit function

end if

'创建新节点信息

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 '写入日志文件

set cnode=nothing

set node2=nothing

set xd=nothing

writeSysLog=9 '说明正常写入了日志信息

end function

'获得当前虚拟目录的名字

function getRoot()

url=Request.ServerVariables("URL")

url=right(url,len(url)-1)

getRoot= mid(url,1,instr(url,"/")-1)

end function

'将一个一位的数字前面加零

function FillZero(str)

ttt=str

if len(str)=1 then

ttt="0" & str

end if

FillZero=ttt

end function

'转化日期,将 一位补上零 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

三、用户格式化的xsl文件:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://www.cccar.com.cn/"

exclude-result-prefixes="msxsl user">

<!-- localized strings -->

<xsl:variable name='ColumnHeader_UserName'>用户</xsl:variable>

<xsl:variable name='ColumnHeader_Time'>时间</xsl:variable>

<xsl:variable name='ColumnHeader_Operate'>操作</xsl:variable>

<xsl:variable name='ColumnHeader_Address'>IP地址</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">

<html>

<head>

<title>

日志查看

</title>

</head>

<body style='margin:10;background-color:#DAE6D8'>

<div align="center">

<table style="{$TableStyle}" width="100%" align="center" cellspacing='0'>

<thead>

<tr height="23">

<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}">

 

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有