上一篇:漫话验证码缘起,程序原理及其他 >>
asp写的日志处理方法
一个书写日志的函数,提供几个参数,用户程序调用这个函数就可以实现日志的记录。日志记录到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}">
<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="23">
<td valign="bottom"><xsl:value-of select="username"/></td>
<td valign="bottom" ><xsl:value-of select="opdate"/></td>
<td valign="bottom" ><xsl:value-of select="operate"/></td>
<td valign="bottom" ><xsl:value-of select="userip"/></td>
</tr>
<tr bgcolor="#999999"><td colspan="4" height="1"></td></tr>
</xsl:for-each>
<tr><td colspan="4" align="right">合计:<xsl:value-of select="count(log)"/> 条 </td></tr>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下一篇:时间、空间性能极优的asp无组件上传类 >>
相关文章:
- · ASP 编程中20个非常有用的例子
- · 对动网论坛作搜索引擎优化
- · 在ASP中按指定参数格式化显示时间的函数。
- · IIS5中的两种服务器端网页重定向方法
- · 验证码的制作和使用(ASP)
- · Global.asa使用手册
- · 初试WAP之wml+ASP查询
- · 用python实现面向对像的ASP程序.
- · 动态的二级伸缩式的菜单,asp编写
- · adodb Stream 详细用法
- · 抓取网页萃取网页内容的代码
- · WEB编程开发常用的代码
- · 一些初学都常用的ASP代码
- · 简单WEB开发规范(修正版)
- · Apache2+PHP4+MySql配置
- · ASP教程之Response对象的使用
- · 编写安全的ASP代码
- · vbscript起步——与窗体的交互,函数的调用
- · Google PR值查询
- · 服务器端动态加载DataGrid控件并设置其属性
- · MD5加密算法 ASP版
- · asp当中判断函数一览
- · 在EditPlus实现asp(VBScript)的自动完成和函数列表
- · 实现会话持久化(Permanent Session)
- · Web用户空件的属性中自定义属性的设置和使用
- · OpenSchema 方法详解
- · 做网页的一些技巧(续)
- · Asp+Sql 个人总结对数据库的各种操作
- · 图片上传的WebForm(自动生成所略图)
- · 在ASP中使用断开的记录集
- · 如何使用ASP制作类似安装向导的页面?
- · ASP与ACCESS数据库
- · 基于ASP的站内多值搜索
- · 基于ASP的站内多值搜索
- · asp使用jmail4.3的模块
- · 用ASP建造自己的聊天室
- · ASP 函数语法速查表
- · 用ASP 实 现 分 页 显 示
