From a58fa8cd57987f75d4015f1187e804c50f5f5208 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Fri, 2 Nov 2018 20:04:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9ALo?= =?UTF-8?q?g=E9=87=8D=E6=9E=84SQL=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=AD=90?= =?UTF-8?q?=E7=B1=BB=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=E7=88=B6=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess.MySQL/Log.cs | 74 +----------------------------- Bootstrap.DataAccess.SQLite/Log.cs | 74 +----------------------------- Bootstrap.DataAccess/Log.cs | 14 ++++-- 3 files changed, 13 insertions(+), 149 deletions(-) diff --git a/Bootstrap.DataAccess.MySQL/Log.cs b/Bootstrap.DataAccess.MySQL/Log.cs index 8b10286a..1b313ab0 100644 --- a/Bootstrap.DataAccess.MySQL/Log.cs +++ b/Bootstrap.DataAccess.MySQL/Log.cs @@ -1,80 +1,10 @@ -using Longbow; -using Longbow.Configuration; -using System; -using System.Collections.Generic; -using System.Data; -using System.Data.Common; - -namespace Bootstrap.DataAccess.MySQL +namespace Bootstrap.DataAccess.MySQL { /// /// /// public class Log : DataAccess.Log { - /// - /// 查询所有日志信息 - /// - /// - /// - public override IEnumerable RetrieveLogs() - { - string sql = "select * from Logs where LogTime > date_add(now(), interval -7 day)"; - List logs = new List(); - DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); - using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd)) - { - while (reader.Read()) - { - logs.Add(new DataAccess.Log() - { - Id = reader[0].ToString(), - CRUD = (string)reader[1], - UserName = (string)reader[2], - LogTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue), - ClientIp = (string)reader[4], - ClientAgent = (string)reader[5], - RequestUrl = (string)reader[6] - }); - } - } - return logs; - } - /// - /// 删除日志信息 - /// - /// - /// - private static void DeleteLogAsync() - { - System.Threading.Tasks.Task.Run(() => - { - string sql = $"delete from Logs where LogTime < date_add(now(), interval -{ConfigurationManager.AppSettings["KeepLogsPeriod"]} month)"; - DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd); - }); - } - /// - /// 保存新增的日志信息 - /// - /// - /// - public override bool SaveLog(DataAccess.Log p) - { - if (p == null) throw new ArgumentNullException("p"); - bool ret = false; - string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, now(), @ClientIp, @ClientAgent, @RequestUrl)"; - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@CRUD", p.CRUD)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", p.UserName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientIp", p.ClientIp)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; - } - DeleteLogAsync(); - return ret; - } + } } diff --git a/Bootstrap.DataAccess.SQLite/Log.cs b/Bootstrap.DataAccess.SQLite/Log.cs index 3040b517..315d353d 100644 --- a/Bootstrap.DataAccess.SQLite/Log.cs +++ b/Bootstrap.DataAccess.SQLite/Log.cs @@ -1,80 +1,10 @@ -using Longbow; -using Longbow.Configuration; -using System; -using System.Collections.Generic; -using System.Data; -using System.Data.Common; - -namespace Bootstrap.DataAccess.SQLite +namespace Bootstrap.DataAccess.SQLite { /// /// /// public class Log : DataAccess.Log { - /// - /// 查询所有日志信息 - /// - /// - /// - public override IEnumerable RetrieveLogs() - { - string sql = "select * from Logs where LogTime > datetime('now', 'localtime', '-7 day')"; - List logs = new List(); - DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); - using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd)) - { - while (reader.Read()) - { - logs.Add(new DataAccess.Log() - { - Id = reader[0].ToString(), - CRUD = (string)reader[1], - UserName = (string)reader[2], - LogTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue), - ClientIp = (string)reader[4], - ClientAgent = (string)reader[5], - RequestUrl = (string)reader[6] - }); - } - } - return logs; - } - /// - /// 删除日志信息 - /// - /// - /// - private static void DeleteLogAsync() - { - System.Threading.Tasks.Task.Run(() => - { - string sql = $"delete from Logs where LogTime < datetime('now', 'localtime', '-{ConfigurationManager.AppSettings["KeepLogsPeriod"]} month')"; - DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd); - }); - } - /// - /// 保存新增的日志信息 - /// - /// - /// - public override bool SaveLog(DataAccess.Log p) - { - if (p == null) throw new ArgumentNullException("p"); - bool ret = false; - string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, datetime('now', 'localtime'), @ClientIp, @ClientAgent, @RequestUrl)"; - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@CRUD", p.CRUD)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", p.UserName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientIp", p.ClientIp)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; - } - DeleteLogAsync(); - return ret; - } + } } diff --git a/Bootstrap.DataAccess/Log.cs b/Bootstrap.DataAccess/Log.cs index 1af582d1..d4a8376f 100644 --- a/Bootstrap.DataAccess/Log.cs +++ b/Bootstrap.DataAccess/Log.cs @@ -1,4 +1,5 @@ -using Longbow.Configuration; +using Longbow; +using Longbow.Configuration; using System; using System.Collections.Generic; using System.Data; @@ -52,9 +53,10 @@ namespace Bootstrap.DataAccess /// public virtual IEnumerable RetrieveLogs() { - string sql = "select * from Logs where DATEDIFF(Week, LogTime, GETDATE()) = 0"; + string sql = "select * from Logs where LogTime > @LogTime"; List logs = new List(); DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@LogTime", DateTime.Now.AddDays(-7), DbType.DateTime)); using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd)) { while (reader.Read()) @@ -82,8 +84,9 @@ namespace Bootstrap.DataAccess { System.Threading.Tasks.Task.Run(() => { - string sql = $"delete from Logs where LogTime < DATEADD(MONTH, -{ConfigurationManager.AppSettings["KeepLogsPeriod"]}, GETDATE())"; + string sql = $"delete from Logs where LogTime < @LogTime"; DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@LogTime", DateTime.Now.AddMonths(0 - LgbConvert.ReadValue(ConfigurationManager.AppSettings["KeepLogsPeriod"], 1)), DbType.DateTime)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd); }); } @@ -94,13 +97,14 @@ namespace Bootstrap.DataAccess /// public virtual bool SaveLog(Log p) { - if (p == null) throw new ArgumentNullException("p"); + if (p == null) throw new ArgumentNullException(nameof(p)); bool ret = false; - string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, GetDate(), @ClientIp, @ClientAgent, @RequestUrl)"; + string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, @LogTime, @ClientIp, @ClientAgent, @RequestUrl)"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@CRUD", p.CRUD)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", p.UserName)); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@LogTime", DateTime.Now)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientIp", p.ClientIp)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ClientAgent", p.ClientAgent)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl));