![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
CYQ.Data 数据框架 应用示例 Aop应用之操作日志
Guide |
|
|
| #TopicOwner |
前言: 在8月份时,那时曾用CYQ.Data 1.2版本演示过一个完整的示例:
留言版:详见:CYQ.Data 数据框架 应用示例 留言版
本篇将使用2.N系列版本,在不改动原来留言版系统一行代码的情况下,实现其数据库操作日志功能。 正文步骤: 一:建表 1:增加一个日志操作表[ActionLogs],这个少不了,表结构如下: 2:创建表的数据库脚本如下: --创建ActionLogs表
CREATE TABLE [dbo].[ActionLogs]( [ID] [int] IDENTITY(1,1) NOT NULL, [UserID] [int] NULL, [Msg] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL, [LogTime] [datetime] NULL CONSTRAINT [DF_ActionLogs_LogTime] DEFAULT (getdate()), CONSTRAINT [PK_ActionLogs] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] 二:建类库项目独立实现操作日志 1:新建一个项目类库起名就叫:AopLog,同时添加对CYQ.Data.DLL的引用[2.N系列版本] 2:新建一个类,继承自CYQ.Data.Aop.IAop接口,并实现之如: public class AopLog : IAop { #region IAop 成员 public void Begin(AopEnum action, string objName, params object[] aopInfo) { //这里先没用到,用于权限判断 } //备注:V4.0之后版本的,不再返回id,而是返回result,根据条件返回MDataRow或MDataTable public void End(AopEnum action, bool success, object id, params object[] aopInfo) { //这里等会要实现 } public IAop GetFromConfig() { return null;//默认这样就行了 } public void OnError(string msg) { //这里先没用到,数据库操作异常时引发 } #endregion } 3:我们将具体实现分流到另一个类处理:新建类:ActionLog.cs,并预先留下方法接口: ![]() ![]() public class ActionLog { public void WriteFill(object id) { } public void WriteInsert(object id) { } #region 其它方法 private int GetUserID() { } private void Write(object id, string msg) { } private bool ContainUrl(string key) { } #endregion } 4:回到AopLog实现方法 ![]() ![]() public class AopLog : IAop { ActionLog log; public AopLog() { log = new ActionLog(); } #region IAop 成员 public void Begin(AopEnum action, string objName, params object[] aopInfo) { } public void End(AopEnum action, bool success, object id, params object[] aopInfo) { if (success) { switch (action) { case AopEnum.Fill: log.WriteFill(id); break; case AopEnum.Insert: log.WriteInsert(id); break; default: break; } } } public IAop GetFromConfig() { return null;//这个默认返回null就行了 } public void OnError(string msg) { //在数据库操作异常时引发 } #endregion } 5:实现ActionLog方法 a:从Session获取用户ID private int GetUserID() { int id=0; if (HttpContext.Current.Session["ID"] != null) { int.TryParse(Convert.ToString(HttpContext.Current.Session["ID"]), out id); } return id; } b:插入表数据方法封装 private void Write(object id,string msg) { int userID = GetUserID(); if (userID == 0) { int.TryParse(Convert.ToString(id), out userID); } if (userID > 0) { MAction action = new MAction(TableNames.ActionLogs); action.SetNoAop();//写日志时,注意关掉Aop功能。 action.Set(ActionLogs.UserID, userID); action.Set(ActionLogs.Msg, msg); action.Insert(); action.Close(); } } c:检测Url是否包含指定字符 private bool ContainUrl(string key) { return System.Web.HttpContext.Current.Request.Url.ToString().ToLower().Contains(key); } d:实现登陆日志记录 public void WriteFill(object id) { if (ContainUrl("login.aspx")) { Write(id,"登陆成功!"); } } e:实现注册日志与留言日志记录 public void WriteInsert(object id) { if (ContainUrl("reg.aspx")) { Write(id,"注册成功!"); } else { Write(id,string.Format("添加一行留言[id={0}]!",id));//需要启用更多分支时,请使用"params object[] aopInfo信息[这个需要修改界面参数代码]" } } 三:切入原有留言版系统 1:将类库生成的AopLog.dll放到留言系统的Bin目录下 2:升级原来CYQ.Data.DLL的1.2版本到最新版本。[完全兼容,直接复盖升级] 3:配置文件appSettings项增加一行配置: 四:运行结果 1:操作登陆、注册、留言!!!数据库记录结果如下图: 五:示例下载 点击下载 [为节省空间,删除了生成的dll,请重新引用dll编绎运行] ![]() |
游客[注册][202.105.205.*]2015/6/1 8:58:15 | #3 | |
![]() | 請問大神V5源碼要多少錢 我是和交流過的 |
游客[注册][122.235.166.*]2013/5/5 22:14:25 | #2 | |
![]() | 很不错的呀 哈哈 学习了 reply: V5版本的AOP更强大,参数更多了。 |
游客[注册][122.234.5.*]2011/2/20 19:16:30 | #1 | |
![]() | 没什么技术含量。 reply: 终于遇到高手了。 |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.