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;
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
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 =>
|
|
|
|
|
{
|
|
|
|
|
string sql = "select * from Logs";
|
|
|
|
|
List<Log> Logs = new List<Log>();
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
Logs.Add(new Log()
|
|
|
|
|
{
|
|
|
|
|
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); }
|
|
|
|
|
return Logs;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
|
|
|
|
|
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool DeleteLog(string ids)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-11-05 15:26:42 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@CRUD", p.CRUD, ParameterDirection.Input));
|
2016-11-05 12:11:16 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName, ParameterDirection.Input));
|
2016-11-05 15:26:42 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientIp", p.ClientIp, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ClientAgent", p.ClientAgent, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl, ParameterDirection.Input));
|
2016-11-05 12:11:16 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
|
|
|
|
CacheCleanUtility.ClearCache(logIds: p.ID == 0 ? "" : p.ID.ToString());
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|