关于日志模块的设计
2010-05-12 19:46 Virus-BeautyCode 阅读(7289) 评论(6) 编辑 收藏 举报目录
1 使用技术以及外部框架
使用.NET平台,引入微软企业库4.1中的日志模块,利用它实现文件日志的记录。
2 详细描述
2.1概况
2.1.1记录的内容
l 用户操作日志,对数据库的操作日志,包括:增加,修改,删除,查询,登录,退出。
l 系统运行异常信息,包括:数据库操作异常,文件资源操作异常,以及其他资源的操作异常。
l 前台对后台服务调用的参数传递,包括:方法的名称,方法的参数和值。
2.1.2日志记录的位置及相应的内容
1. 数据库
记录用户操作日志,对数据库的操作;
记录系统运行的异常信息
记录用户登录、退出系统的行为
2. 文件1
记录用户操作日志,对数据库的操作;
记录系统运行的异常信息
记录用户登录、退出系统的行为
3. 文件2
前台调用后台服务的方法和传递的参数
2.1.3日志的类型
/// <summary>
/// 日志类型
/// </summary>
[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
}
2.1.4日志功能的配置
使用web.config文件进行系统日志功能的配置,包括是否记录日志,是否记录异常信息,是否某一种类型的日志等一些开关的控制。
1. Web.config文件中的系统日志配置。
<configuration>
<configSections>
<section name="logSettings" type="KB.DSN.Entity.LoggingSection, KB.DSN.Entity.Server"/>
</configSections>
<logSettings isLogging="true">
<wcfCall isLogging="true"></wcfCall>
<database isLogging="true">
<add isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</add>
<modify isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</modify>
<delete isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</delete>
<get isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</get>
<logon isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</logon>
<logoff isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</logoff>
</database>
<file isLogging="true">
<add isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</add>
<modify isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</modify>
<delete isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</delete>
<get isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</get>
<logon isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="true"></normal>
</logon>
<logoff isLogging="true">
<exception isLogging="true"></exception>
<normal isLogging="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; }
}
}
}
2.1.5配置节类的用法
ConfigurationSection section = ConfigurationManager.GetSection("logSettings") as BeautyCode.Entity.LoggingSection;
获取要防止失败,获取配置信息失败的话,在后面记录日志的时候就全部记录。
2.2数据库日志
三张表:LogInfo操作日志,LogonLog登录、退出日志,OnlineUser在线用户
数据库日志记录的方法写在数据访问层,在操作数据库之前记录日志,操作数据库的时候使用try 。。。catch。。。,遇到异常的时候也需要记录数据库日志。
2.3文件日志
2.3.1记录方式
采用本地文件的记录方式,记录在应用服务器的本地IIS目录。每年一个文件夹,里面每个月一个文件夹,在里面每天一个文件夹,上午一个文件,下午一个文件。
2.3.2文件日志的格式
1. 文本文件的格式
<?xml version="1.0" encoding="utf-8" ?>
<logs>
<!--用户操作数据库记录的日志-->
<log>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logType>2</logType>
<logTime>2009-9-9</logTime>
<logObjectType>2</logObjectType>
<logObjectID>B3D8E3BB-83B2-4553-A487-7CFDDD9503aa</logObjectID>
<operation>
<title>存储过程及其参数,或者是SQL语句</title>
<procedure>Proc_Order_FindAll</procedure>
<parameters>
<parameter name="@Username" value="shiwenbin"></parameter>
<parameter name="@UserType" value="1"></parameter>
</parameters>
</operation>
<opeTable>order</opeTable>
<before>
<name value="1"></name>
<age value="52"></age>
</before>
<after>
<name value="2"></name>
<age value="50"></age>
</after>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</log>
<!--用户登录系统记录的日志-->
<logon>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logonTime>2009-9-9</logonTime>
<logoffTime></logoffTime>
<logonName></logonName>
<userTyype></userTyype>
<communicationCode></communicationCode>
<ip></ip>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</logon>
<!--用户退出系统记录的日志-->
<logoff>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logonTime></logonTime>
<logoffTime>2009-9-9</logoffTime>
<logonName></logonName>
<userTyype></userTyype>
<communicationCode></communicationCode>
<ip></ip>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</logoff>
<!--前台调用后台服务的时候记录的日志-->
<wcfCall>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id></id>
<userType></userType>
<username></username>
<call>
<methodName>GetTopCropClass</methodName>
<parameters>
<parameter name="@Username" value="shiwenbin"></parameter>
<parameter name="@UserType" value="2"></parameter>
</parameters>
</call>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</wcfCall>
</logs>
2. xml文件的格式
<?xml version="1.0" encoding="utf-8" ?>
<logs>
<!--用户操作数据库记录的日志-->
<log>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logType>2</logType>
<logTime>2009-9-9</logTime>
<logObjectType>2</logObjectType>
<logObjectID>B3D8E3BB-83B2-4553-A487-7CFDDD9503aa</logObjectID>
<operation>
<title>存储过程及其参数,或者是SQL语句</title>
<procedure>Proc_Order_FindAll</procedure>
<parameters>
<parameter name="@Username" value="shiwenbin"></parameter>
<parameter name="@UserType" value="1"></parameter>
</parameters>
</operation>
<opeTable>order</opeTable>
<before>
<name value="1"></name>
<age value="52"></age>
</before>
<after>
<name value="2"></name>
<age value="50"></age>
</after>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</log>
<!--用户登录系统记录的日志-->
<logon>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logonTime>2009-9-9</logonTime>
<logoffTime></logoffTime>
<logonName></logonName>
<userTyype></userTyype>
<communicationCode></communicationCode>
<ip></ip>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</logon>
<!--用户退出系统记录的日志-->
<logoff>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id>B3D8E3BB-83B2-4553-A487-7CFDDD950368</id>
<logonTime></logonTime>
<logoffTime>2009-9-9</logoffTime>
<logonName></logonName>
<userTyype></userTyype>
<communicationCode></communicationCode>
<ip></ip>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</logoff>
<!--前台调用后台服务的时候记录的日志-->
<wcfCall>
<Timestamp>2010-5-5 11:43:19</Timestamp>
<Message>
<id></id>
<userType></userType>
<username></username>
<call>
<methodName>GetTopCropClass</methodName>
<parameters>
<parameter name="@Username" value="shiwenbin"></parameter>
<parameter name="@UserType" value="2"></parameter>
</parameters>
</call>
</Message>
<Category> Data Access Events</Category>
<Priority>-1</Priority>
<EventId> 1</EventId>
<Severity> Information</Severity>
<Title></Title>
<Machine> SHIWB</Machine>
<ApplicationDomain> 89d819cc-10-129175333882812500</ApplicationDomain>
<ProcessId>5772</ProcessId>
<ProcessName>C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe</ProcessName>
<Win32ThreadId> 6840</Win32ThreadId>
<ThreadName></ThreadName>
<ExtendedProperties></ExtendedProperties>
</wcfCall>
</logs>