系统日志后台功能

This commit is contained in:
LRan 2016-10-28 16:57:27 +08:00
parent af3ce6d625
commit 13b1a8b49e
7 changed files with 240 additions and 0 deletions

View File

@ -31,6 +31,7 @@
<add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" /> <add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" />
<add key="RoleData-CodeRoleHelper-Navigation-" interval="600" desc="菜单角色信息缓存"/> <add key="RoleData-CodeRoleHelper-Navigation-" interval="600" desc="菜单角色信息缓存"/>
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" /> <add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
<add key="LogData-CodeMenuHelper" interval="600" desc="日志信息缓存" />
<add key="DictData-CodeDictHelper" interval="600" desc="字典信息缓存" /> <add key="DictData-CodeDictHelper" interval="600" desc="字典信息缓存" />
</cacheManager> </cacheManager>

View File

@ -61,6 +61,8 @@
<Compile Include="DictHelper.cs" /> <Compile Include="DictHelper.cs" />
<Compile Include="Group.cs" /> <Compile Include="Group.cs" />
<Compile Include="GroupHelper.cs" /> <Compile Include="GroupHelper.cs" />
<Compile Include="Log.cs" />
<Compile Include="LogHelper.cs" />
<Compile Include="Menu.cs" /> <Compile Include="Menu.cs" />
<Compile Include="MenuHelper.cs" /> <Compile Include="MenuHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -0,0 +1,51 @@
using System;
namespace Bootstrap.DataAccess
{
public class Log
{
/// <summary>
/// 获得/设置 操作日志主键ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 获得/设置 操作类型
/// </summary>
public int OperationType { get; set; }
/// <summary>
/// 获得/设置 用户ID
/// </summary>
public int UserID { get; set; }
/// <summary>
/// 获得/设置 操作时间
/// </summary>
public DateTime OperationTime { get; set; }
/// <summary>
/// 获得/设置 操作表表名
/// </summary>
public string TableName { get; set; }
/// <summary>
/// 获得/设置 操作内容
/// </summary>
public string BusinessName { get; set; }
/// <summary>
/// 获得/设置 操作表的主键
/// </summary>
public string PrimaryKey { get; set; }
/// <summary>
/// 获得/设置 sql语句
/// </summary>
public string SqlText { get; set; }
/// <summary>
/// 获得/设置 操作者Ip
/// </summary>
public string OperationIp { get; set; }
}
}

View File

@ -0,0 +1,118 @@
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 class LogHelper
{
private const string LogDataKey = "LogData-CodeLogHelper";
/// <summary>
/// 查询所有日志信息
/// </summary>
/// <param name="tId"></param>
/// <returns></returns>
public static IEnumerable<Log> RetrieveLogs(string tId = null)
{
string sql = "select * from Logs";
var ret = CacheManager.GetOrAdd(LogDataKey, CacheSection.RetrieveIntervalByKey(LogDataKey), key =>
{
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],
OperationType = (int)reader[1],
UserID = (int)reader[2],
OperationTime = (DateTime)reader[3],
TableName = (string)reader[4],
BusinessName = (string)reader[5],
PrimaryKey = (string)reader[6],
SqlText = (string)reader[7],
OperationIp = (string)reader[8],
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Logs;
}, CacheSection.RetrieveDescByKey(LogDataKey));
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>
public static bool saveLog(Log p)
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
string sql = "Insert Into Logs (OperationType, UserID,OperationTime,TableName,BusinessName,PrimaryKey,SqlText,OperationIp) Values (@OperationType, @UserID,@OperationTime,@TableName,@BusinessName,@PrimaryKey,@SqlText,@OperationIp)";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", p.UserID, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@TableName", p.TableName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@BusinessName", p.BusinessName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PrimaryKey", p.PrimaryKey, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@SqlText", p.SqlText, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
//更新缓存
private static void ClearCache()
{
CacheManager.Clear(key => key == LogDataKey);
}
}
}

View File

@ -59,6 +59,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GroupHelperTests.cs" /> <Compile Include="GroupHelperTests.cs" />
<Compile Include="MenuHelperTests.cs" /> <Compile Include="MenuHelperTests.cs" />
<Compile Include="LogHelperTests.cs" />
<Compile Include="UserHelperTests.cs" /> <Compile Include="UserHelperTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,40 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bootstrap.DataAccess.Tests
{
[TestClass]
public class LogHelperTests
{
[TestMethod]
public void RetrieveLogsTest()
{
var result = LogHelper.RetrieveLogs("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句");
result = LogHelper.RetrieveLogs();
Assert.IsTrue(result.Count() >= 0, "带有参数的LogHelper.RetrieveLogs方法调用失败请检查数据库连接或者数据库SQL语句");
}
[TestMethod]
public void saveLogTest()
{
Log p = new Log();
p.OperationType = 1;
p.UserID = 1;
p.OperationTime = System.DateTime.Now;
p.TableName = "日志";
p.BusinessName = "新增日志信息";
p.PrimaryKey = "ID";
p.SqlText = "Insert Into Logs";
p.OperationIp = "0.0.0.0";
var result = LogHelper.saveLog(p);
Assert.IsTrue(result, "新增日志信息出错");
}
[TestMethod]
public void DeleteLogTest()
{
string p = "2";
Assert.IsTrue(LogHelper.DeleteLog(p),"删除日志信息出错");
}
}
}

View File

@ -144,6 +144,33 @@ EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门名称'
GO GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Groups', @level2type=N'COLUMN',@level2name=N'Description' EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Groups', @level2type=N'COLUMN',@level2name=N'Description'
GO GO
/****** Object: Table [dbo].[Logs] Script Date: 10/28/2016 16:39:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Logs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OperationType] [int] NULL,
[UserID] [int] NULL,
[OperationTime] [datetime] NULL,
[TableName] [nvarchar](50) NULL,
[BusinessName] [nvarchar](50) NULL,
[PrimaryKey] [nvarchar](50) NULL,
[SqlText] [nvarchar](max) NULL,
[OperationIp] [nvarchar](50) NULL,
CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Logs', @level2type=N'COLUMN',@level2name=N'ID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Logs', @level2type=N'COLUMN',@level2name=N'OperationType'
GO
CREATE TABLE [dbo].[Navigations]( CREATE TABLE [dbo].[Navigations](
[ID] [int] IDENTITY(1,1) NOT NULL, [ID] [int] IDENTITY(1,1) NOT NULL,
[ParentId] [int] NOT NULL, [ParentId] [int] NOT NULL,