2018-10-19 23:09:52 +08:00
|
|
|
|
using Longbow;
|
|
|
|
|
using Longbow.Cache;
|
|
|
|
|
using Longbow.Configuration;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.SQLite
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Log : DataAccess.Log
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IEnumerable<DataAccess.Log> RetrieveLogs(string tId = null)
|
|
|
|
|
{
|
|
|
|
|
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, key =>
|
|
|
|
|
{
|
|
|
|
|
string sql = "select * from Logs where LogTime > datetime('now', 'localtime', '-7 day')";
|
|
|
|
|
List<Log> logs = new List<Log>();
|
2018-10-20 22:25:53 +08:00
|
|
|
|
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
logs.Add(new Log()
|
|
|
|
|
{
|
|
|
|
|
Id = LgbConvert.ReadValue(reader[0], 0),
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private void DeleteLogAsync()
|
|
|
|
|
{
|
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
string sql = $"delete from Logs where LogTime < datetime('now', 'localtime', '-{ConfigurationManager.AppSettings["KeepLogsPeriod"]} month')";
|
2018-10-20 22:25:53 +08:00
|
|
|
|
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新增的日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override bool SaveLog(Bootstrap.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)";
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-20 22:25:53 +08:00
|
|
|
|
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;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
CacheManager.Clear(RetrieveLogsDataKey);
|
|
|
|
|
DeleteLogAsync();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|