2016-11-04 16:06:40 +08:00
|
|
|
|
using Longbow;
|
|
|
|
|
using Longbow.Caching;
|
2016-10-28 16:57:27 +08:00
|
|
|
|
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;
|
2016-11-04 15:22:44 +08:00
|
|
|
|
using System.Web;
|
2016-10-28 16:57:27 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
public static class LogHelper
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
private const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
|
2016-10-28 16:57:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Log> RetrieveLogs(string tId = null)
|
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
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
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
while (reader.Read())
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
Logs.Add(new Log()
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
ID = (int)reader[0],
|
|
|
|
|
OperationType = (int)reader[1],
|
|
|
|
|
UserName = (string)reader[2],
|
|
|
|
|
OperationTime = (DateTime)reader[3],
|
|
|
|
|
OperationIp = (string)reader[4],
|
|
|
|
|
Remark = (string)reader[5],
|
|
|
|
|
OperationModule = (string)reader[6]
|
|
|
|
|
});
|
2016-10-28 16:57:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-04 16:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return Logs;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveLogsDataKey));
|
2016-10-28 16:57:27 +08:00
|
|
|
|
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);
|
|
|
|
|
ClearCache();
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新增的日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-11-01 21:03:11 +08:00
|
|
|
|
public static bool SaveLog(Log p)
|
2016-10-28 16:57:27 +08:00
|
|
|
|
{
|
|
|
|
|
if (p == null) throw new ArgumentNullException("p");
|
|
|
|
|
bool ret = false;
|
2016-11-04 15:22:44 +08:00
|
|
|
|
string sql = "Insert Into Logs (OperationType, UserName,OperationTime,OperationIp,Remark,OperationModule) Values (@OperationType, @UserName,@OperationTime,@OperationIp,@Remark,@OperationModule)";
|
2016-10-28 16:57:27 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input));
|
2016-11-02 15:35:25 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName, ParameterDirection.Input));
|
2016-11-04 15:22:44 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp == null ? "" : p.OperationIp, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Remark", p.Remark == null ? "" : p.Remark, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationModule", p.OperationModule == null ? "" : p.OperationModule, ParameterDirection.Input));
|
2016-10-28 16:57:27 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
ClearCache();
|
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
//更新缓存
|
|
|
|
|
private static void ClearCache()
|
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
CacheManager.Clear(key => key == RetrieveLogsDataKey);
|
2016-10-28 16:57:27 +08:00
|
|
|
|
}
|
2016-11-04 15:22:44 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取客户端IP地址
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetClientIp()
|
|
|
|
|
{
|
|
|
|
|
HttpRequest request = HttpContext.Current.Request;
|
|
|
|
|
string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
result = request.ServerVariables["REMOTE_ADDR"];
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
result = request.UserHostAddress;
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
result = "0.0.0.0";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-10-28 16:57:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|