2016-11-05 12:11:16 +08:00
|
|
|
|
using Longbow.Caching;
|
|
|
|
|
using Longbow.Caching.Configuration;
|
|
|
|
|
using Longbow.ExceptionManagement;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
public static class LogHelper
|
|
|
|
|
{
|
|
|
|
|
internal const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Log> RetrieveLogs(string tId = null)
|
|
|
|
|
{
|
|
|
|
|
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveLogsDataKey), key =>
|
|
|
|
|
{
|
2016-12-09 13:01:06 +08:00
|
|
|
|
string sql = "select top 1000 * from Logs";
|
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);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
logs.Add(new Log()
|
2016-11-05 12:11:16 +08:00
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
Id = (int)reader[0],
|
2016-11-05 15:26:42 +08:00
|
|
|
|
CRUD = (string)reader[1],
|
2016-11-05 12:11:16 +08:00
|
|
|
|
UserName = (string)reader[2],
|
2016-11-05 15:26:42 +08:00
|
|
|
|
LogTime = (DateTime)reader[3],
|
|
|
|
|
ClientIp = (string)reader[4],
|
|
|
|
|
ClientAgent = (string)reader[5],
|
|
|
|
|
RequestUrl = (string)reader[6]
|
2016-11-05 12:11:16 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
2017-03-30 16:15:45 +08:00
|
|
|
|
return logs;
|
2016-11-05 12:11:16 +08:00
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
|
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>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool DeleteLog(string ids)
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return false;
|
2016-11-05 12:11:16 +08:00
|
|
|
|
bool ret = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs where ID in ({0})", ids);
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
CacheCleanUtility.ClearCache(logIds: ids);
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <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)";
|
2016-11-05 12:11:16 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
2017-03-30 16:15:45 +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));
|
2016-11-05 12:11:16 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2017-03-30 16:15:45 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(logIds: p.Id == 0 ? string.Empty : p.Id.ToString());
|
2016-11-05 12:11:16 +08:00
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|