目录
1 使用技术以及外部框架
2 详细描述
2.1概况
2.1.1记录的内容
2.1.2日志记录的位置及相应的内容
2.1.3日志的类型
2.1.4日志功能的配置
2.1.5配置节类的用法
2.2数据库日志
2.3文件日志
2.3.1记录方式
2.3.2文件日志的格式
使用.NET平台,引入微软企业库4.1中的日志模块,利用它实现文件日志的记录。
l 用户操作日志,对数据库的操作日志,包括:增加,修改,删除,查询,登录,退出。
l 系统运行异常信息,包括:数据库操作异常,文件资源操作异常,以及其他资源的操作异常。
l 前台对后台服务调用的参数传递,包括:方法的名称,方法的参数和值。
1. 数据库
记录用户操作日志,对数据库的操作;
记录系统运行的异常信息
记录用户登录、退出系统的行为
2. 文件1
记录用户操作日志,对数据库的操作;
记录系统运行的异常信息
记录用户登录、退出系统的行为
3. 文件2
前台调用后台服务的方法和传递的参数
///
///日志类型
///
[DataContract]
[Flags]
public enum LogType
{
[EnumMember]
[EnumDescription("添加成功")]
AddSuccess = 1,
[EnumMember]
[EnumDescription("修改成功")]
ModifySuccess = 2,
[EnumMember]
[EnumDescription("删除成功")]
DeleteSuccess = 4,
[EnumMember]
[EnumDescription("获取成功")]
GetSuccess = 8,
[EnumMember]
[EnumDescription("登录成功")]
LogonSuccess = 16,
[EnumMember]
[EnumDescription("退出成功")]
LogoffSuccess = 32,
[EnumMember]
[EnumDescription("添加失败")]
AddFail = 64,
[EnumMember]
[EnumDescription("修改失败")]
ModifyFail = 128,
[EnumMember]
[EnumDescription("删除失败")]
DeleteFail = 256,
[EnumMember]
[EnumDescription("获取失败")]
GetFail = 512,
[EnumMember]
[EnumDescription("登录失败")]
LogonFail = 1024,
[EnumMember]
[EnumDescription("退出失败")]
LogoffFail = 2048,
[EnumMember]
[EnumDescription("未知")]
Unknow = 4096,
[EnumMember]
[EnumDescription("事务成功")]
TransactionSuccess = 8192,
[EnumMember]
[EnumDescription("事务失败")]
TransactionFail = 16384
}
使用web.config文件进行系统日志功能的配置,包括是否记录日志,是否记录异常信息,是否某一种类型的日志等一些开关的控制。
1. Web.config文件中的系统日志配置。
<configuration>
<configSections>
<sectionname="logSettings"type="KB.DSN.Entity.LoggingSection, KB.DSN.Entity.Server"/>
configSections>
<logSettingsisLogging="true">
<wcfCallisLogging="true">wcfCall>
<databaseisLogging="true">
<addisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
add>
<modifyisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
modify>
<deleteisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
delete>
<getisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
get>
<logonisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
logon>
<logoffisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
logoff>
database>
<fileisLogging="true">
<addisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
add>
<modifyisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
modify>
<deleteisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
delete>
<getisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
get>
<logonisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
logon>
<logoffisLogging="true">
<exceptionisLogging="true">exception>
<normalisLogging="true">normal>
logoff>
file>
logSettings>
configuration>
2. web.config文件中日志配置节对应的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace KB.DSN.Entity
{
public class LoggingSection : ConfigurationSection
{
public LoggingSection()
{ }
[ConfigurationProperty ("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set{this["isLogging"]=value;}
}
[ConfigurationProperty ("database")]
public DatabaseElement Database
{
get{ return (DatabaseElement)this["database"];}
set{this["database"]=value;}
}
[ConfigurationProperty("file")]
public FileElement File
{
get { return (FileElement)this["file"]; }
set { this["file"] = value; }
}
[ConfigurationProperty("wcfCall")]
public WCFCallElement WCFCall
{
get { return (WCFCallElement)this["wcfCall"]; }
set { this["wcfCall"] = value; }
}
}
public class WCFCallElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
}
public class DatabaseElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("add")]
public AddElement Add
{
get { return (AddElement)this["add"]; }
set { this["add"] = value; }
}
[ConfigurationProperty("modify")]
public ModifyElement Modify
{
get { return (ModifyElement)this["modify"]; }
set { this["modify"] = value; }
}
[ConfigurationProperty("delete")]
public DeleteElement Delete
{
get { return (DeleteElement)this["delete"]; }
set { this["delete"] = value; }
}
[ConfigurationProperty("get")]
public GetElement Get
{
get { return (GetElement)this["get"]; }
set { this["get"] = value; }
}
[ConfigurationProperty("logon")]
public LogonElement Logon
{
get { return (LogonElement)this["logon"]; }
set { this["logon"] = value; }
}
[ConfigurationProperty("logoff")]
public LogoffElement Logoff
{
get { return (LogoffElement)this["logoff"]; }
set { this["logoff"] = value; }
}
}
public class FileElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("add")]
public AddElement Add
{
get { return (AddElement)this["add"]; }
set { this["add"] = value; }
}
[ConfigurationProperty("modify")]
public ModifyElement Modify
{
get { return (ModifyElement)this["modify"]; }
set { this["modify"] = value; }
}
[ConfigurationProperty("delete")]
public DeleteElement Delete
{
get { return (DeleteElement)this["delete"]; }
set { this["delete"] = value; }
}
[ConfigurationProperty("get")]
public GetElement Get
{
get { return (GetElement)this["get"]; }
set { this["get"] = value; }
}
[ConfigurationProperty("logon")]
public LogonElement Logon
{
get { return (LogonElement)this["logon"]; }
set { this["logon"] = value; }
}
[ConfigurationProperty("logoff")]
public LogoffElement Logoff
{
get { return (LogoffElement)this["logoff"]; }
set { this["logoff"] = value; }
}
}
public class ExceptionElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
}
public class NormalElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
}
public class AddElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty ("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
public class ModifyElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
public class DeleteElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
public class GetElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
public class LogonElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
public class LogoffElement : ConfigurationElement
{
[ConfigurationProperty("isLogging")]
public bool IsLogging
{
get { return (bool)this["isLogging"]; }
set { this["isLogging"] = value; }
}
[ConfigurationProperty("normal")]
public NormalElement Normal
{
get { return (NormalElement)this["normal"]; }
set { this["normal"] = value; }
}
[ConfigurationProperty("exception")]
public ExceptionElement Exception
{
get { return (ExceptionElement)this["exception"]; }
set { this["exception"] = value; }
}
}
}
ConfigurationSection section = ConfigurationManager.GetSection("logSettings") as BeautyCode.Entity.LoggingSection;
获取要防止失败,获取配置信息失败的话,在后面记录日志的时候就全部记录。
三张表:LogInfo操作日志,LogonLog登录、退出日志,OnlineUser在线用户
数据库日志记录的方法写在数据访问层,在操作数据库之前记录日志,操作数据库的时候使用try 。。。catch。。。,遇到异常的时候也需要记录数据库日志。
采用本地文件的记录方式,记录在应用服务器的本地IIS目录。每年一个文件夹,里面每个月一个文件夹,在里面每天一个文件夹,上午一个文件,下午一个文件。
1. 文本文件的格式
xmlversion="1.0"encoding="utf-8" ?>
<logs>
<log>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logType>2logType>
<logTime>2009-9-9logTime>
<logObjectType>2logObjectType>
<logObjectID>B3D8E3BB-83B2-4553-A487-7CFDDD9503aalogObjectID>
<operation>
<title>存储过程及其参数,或者是SQL语句title>
<procedure>Proc_Order_FindAllprocedure>
<parameters>
<parametername="@Username"value="shiwenbin">parameter>
<parametername="@UserType"value="1">parameter>
parameters>
operation>
<opeTable>orderopeTable>
<before>
<namevalue="1">name>
<agevalue="52">age>
before>
<after>
<namevalue="2">name>
<agevalue="50">age>
after>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
log>
<logon>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logonTime>2009-9-9logonTime>
<logoffTime>logoffTime>
<logonName>logonName>
<userTyype>userTyype>
<communicationCode>communicationCode>
<ip>ip>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
logon>
<logoff>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logonTime>logonTime>
<logoffTime>2009-9-9logoffTime>
<logonName>logonName>
<userTyype>userTyype>
<communicationCode>communicationCode>
<ip>ip>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
logoff>
<wcfCall>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>id>
<userType>userType>
<username>username>
<call>
<methodName>GetTopCropClassmethodName>
<parameters>
<parametername="@Username"value="shiwenbin">parameter>
<parametername="@UserType"value="2">parameter>
parameters>
call>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
wcfCall>
logs>
2. xml文件的格式
xmlversion="1.0"encoding="utf-8" ?>
<logs>
<log>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logType>2logType>
<logTime>2009-9-9logTime>
<logObjectType>2logObjectType>
<logObjectID>B3D8E3BB-83B2-4553-A487-7CFDDD9503aalogObjectID>
<operation>
<title>存储过程及其参数,或者是SQL语句title>
<procedure>Proc_Order_FindAllprocedure>
<parameters>
<parametername="@Username"value="shiwenbin">parameter>
<parametername="@UserType"value="1">parameter>
parameters>
operation>
<opeTable>orderopeTable>
<before>
<namevalue="1">name>
<agevalue="52">age>
before>
<after>
<namevalue="2">name>
<agevalue="50">age>
after>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
log>
<logon>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logonTime>2009-9-9logonTime>
<logoffTime>logoffTime>
<logonName>logonName>
<userTyype>userTyype>
<communicationCode>communicationCode>
<ip>ip>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
logon>
<logoff>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368id>
<logonTime>logonTime>
<logoffTime>2009-9-9logoffTime>
<logonName>logonName>
<userTyype>userTyype>
<communicationCode>communicationCode>
<ip>ip>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
logoff>
<wcfCall>
<Timestamp>2010-5-5 11:43:19Timestamp>
<Message>
<id>id>
<userType>userType>
<username>username>
<call>
<methodName>GetTopCropClassmethodName>
<parameters>
<parametername="@Username"value="shiwenbin">parameter>
<parametername="@UserType"value="2">parameter>
parameters>
call>
Message>
<Category> Data Access EventsCategory>
<Priority>-1Priority>
<EventId> 1EventId>
<Severity> InformationSeverity>
<Title>Title>
<Machine> SHIWBMachine>
<ApplicationDomain> 89d819cc-10-129175333882812500ApplicationDomain>
<ProcessId>5772ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exeProcessName>
<Win32ThreadId> 6840Win32ThreadId>
<ThreadName>ThreadName>
<ExtendedProperties>ExtendedProperties>
wcfCall>
logs>
【Blog】http://virusswb.cnblogs.com/
【MSN】[email protected]
【说明】转载请标明出处,谢谢