2018-10-30 22:11:27 +08:00
|
|
|
|
using Longbow.Configuration;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using System;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
|
2016-11-05 15:26:42 +08:00
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
2018-10-21 10:02:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-11-05 15:26:42 +08:00
|
|
|
|
public class Log
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得/设置 操作日志主键ID
|
|
|
|
|
/// </summary>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public string Id { get; set; }
|
2016-11-05 15:26:42 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得/设置 操作类型
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string CRUD { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得/设置 用户名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得/设置 操作时间
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTime LogTime { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得/设置 客户端IP
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ClientIp { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取/设置 客户端信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ClientAgent { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取/设置 请求网址
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string RequestUrl { get; set; }
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public virtual IEnumerable<Log> RetrieveLogs()
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
string sql = "select * from Logs where DATEDIFF(Week, LogTime, GETDATE()) = 0";
|
|
|
|
|
List<Log> logs = new List<Log>();
|
|
|
|
|
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
while (reader.Read())
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
logs.Add(new Log()
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-30 13:07:29 +08:00
|
|
|
|
Id = reader[0].ToString(),
|
2018-10-28 15:03:59 +08:00
|
|
|
|
CRUD = (string)reader[1],
|
|
|
|
|
UserName = (string)reader[2],
|
|
|
|
|
LogTime = (DateTime)reader[3],
|
|
|
|
|
ClientIp = (string)reader[4],
|
|
|
|
|
ClientAgent = (string)reader[5],
|
|
|
|
|
RequestUrl = (string)reader[6]
|
|
|
|
|
});
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
2018-10-28 15:03:59 +08:00
|
|
|
|
}
|
|
|
|
|
return logs;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 22:11:27 +08:00
|
|
|
|
private static void DeleteLogAsync()
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
string sql = $"delete from Logs where LogTime < DATEADD(MONTH, -{ConfigurationManager.AppSettings["KeepLogsPeriod"]}, GETDATE())";
|
|
|
|
|
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>
|
2018-10-21 10:02:59 +08:00
|
|
|
|
public virtual bool SaveLog(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, GetDate(), @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;
|
|
|
|
|
}
|
2016-11-05 15:26:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|