2018-06-07 00:45:47 +08:00
|
|
|
|
using Longbow.Cache;
|
2018-09-09 14:21:16 +08:00
|
|
|
|
using Longbow.Configuration;
|
2016-11-05 12:11:16 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
public static class LogHelper
|
|
|
|
|
{
|
2018-09-09 19:38:27 +08:00
|
|
|
|
private const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
|
2016-11-05 12:11:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Log> RetrieveLogs(string tId = null)
|
|
|
|
|
{
|
2017-04-05 11:54:21 +08:00
|
|
|
|
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, key =>
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-09 14:16:38 +08:00
|
|
|
|
string sql = "select * from Logs where DATEDIFF(Week, LogTime, GETDATE()) = 0";
|
2017-03-30 16:15:45 +08:00
|
|
|
|
List<Log> logs = new List<Log>();
|
2016-11-05 12:11:16 +08:00
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
2018-09-08 19:57:42 +08:00
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-08 19:57:42 +08:00
|
|
|
|
while (reader.Read())
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-08 19:57:42 +08:00
|
|
|
|
logs.Add(new Log()
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-08 19:57:42 +08:00
|
|
|
|
Id = (int)reader[0],
|
|
|
|
|
CRUD = (string)reader[1],
|
|
|
|
|
UserName = (string)reader[2],
|
|
|
|
|
LogTime = (DateTime)reader[3],
|
|
|
|
|
ClientIp = (string)reader[4],
|
|
|
|
|
ClientAgent = (string)reader[5],
|
|
|
|
|
RequestUrl = (string)reader[6]
|
|
|
|
|
});
|
2016-11-05 12:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-30 16:15:45 +08:00
|
|
|
|
return logs;
|
2017-04-05 11:54:21 +08:00
|
|
|
|
});
|
2017-03-30 16:15:45 +08:00
|
|
|
|
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase));
|
2016-11-05 12:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除日志信息
|
|
|
|
|
/// </summary>
|
2018-06-08 12:54:05 +08:00
|
|
|
|
/// <param name="value"></param>
|
2016-11-05 12:11:16 +08:00
|
|
|
|
/// <returns></returns>
|
2018-09-09 14:21:16 +08:00
|
|
|
|
public static void DeleteLogAsync()
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-09 14:21:16 +08:00
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-09 14:21:16 +08:00
|
|
|
|
string sql = $"delete from Logs where LogTime < DATEADD(MONTH, -{ConfigurationManager.AppSettings["KeepLogsPeriod"]}, GETDATE())";
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
});
|
2016-11-05 12:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新增的日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool SaveLog(Log p)
|
|
|
|
|
{
|
|
|
|
|
if (p == null) throw new ArgumentNullException("p");
|
|
|
|
|
bool ret = false;
|
2016-11-05 15:26:42 +08:00
|
|
|
|
string sql = "Insert Into Logs (CRUD, UserName, LogTime, ClientIp, ClientAgent, RequestUrl) Values (@CRUD, @UserName, GetDate(), @ClientIp, @ClientAgent, @RequestUrl)";
|
2018-09-08 19:57:42 +08:00
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2018-09-08 19:57:42 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@CRUD", p.CRUD));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
|
|
|
|
|
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
|
2016-11-05 12:11:16 +08:00
|
|
|
|
}
|
2018-09-09 19:38:27 +08:00
|
|
|
|
CacheManager.Clear(RetrieveLogsDataKey);
|
2018-09-09 14:21:16 +08:00
|
|
|
|
DeleteLogAsync();
|
2016-11-05 12:11:16 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|