增加功能:访问日志增加保存时长设置 closed #ITRQL

#Issue
https://gitee.com/LongbowEnterprise/dashboard/issues?id=ITRQL
This commit is contained in:
Argo Zhang 2019-03-18 17:26:12 +08:00
parent 106c77a26c
commit ac8bc82978
11 changed files with 164 additions and 117 deletions

View File

@ -13,13 +13,11 @@ namespace Bootstrap.Client.DataAccess
/// </summary>
public static class DictHelper
{
/// <summary>
///
/// </summary>
/// <summary>
/// 缓存索引BootstrapAdmin后台清理缓存时使用
/// </summary>
public const string RetrieveDictsDataKey = "BootstrapDict-RetrieveDicts";
/// <summary>
///
/// </summary>
@ -28,6 +26,7 @@ namespace Bootstrap.Client.DataAccess
{
return RetrieveAppName("个人中心地址");
}
/// <summary>
///
/// </summary>
@ -36,6 +35,7 @@ namespace Bootstrap.Client.DataAccess
{
return RetrieveAppName("系统设置地址");
}
/// <summary>
///
/// </summary>
@ -44,6 +44,7 @@ namespace Bootstrap.Client.DataAccess
{
return RetrieveAppName("网站标题");
}
/// <summary>
///
/// </summary>
@ -52,6 +53,7 @@ namespace Bootstrap.Client.DataAccess
{
return RetrieveAppName("网站页脚");
}
/// <summary>
///
/// </summary>

View File

@ -1,4 +1,4 @@
using Longbow.Web.Mvc;
using Longbow.Web.Mvc;
using MongoDB.Driver;
using PetaPoco;
using System;
@ -18,38 +18,38 @@ namespace Bootstrap.DataAccess.MongoDB
public override Page<DataAccess.Trace> Retrieves(PaginationOption po, DateTime? startTime, DateTime? endTime)
{
// filter
var filterBuilder = Builders<DataAccess.Trace>.Filter;
var filterBuilder = Builders<DataAccess.Trace>.Filter;
var filter = filterBuilder.Empty;
if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddDays(-7));
// sort
var sortBuilder = Builders<DataAccess.Trace>.Sort;
SortDefinition<DataAccess.Trace> sort = null;
switch (po.Sort)
{
case "LogTime":
if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
// sort
var sortBuilder = Builders<DataAccess.Trace>.Sort;
SortDefinition<DataAccess.Trace> sort = null;
switch (po.Sort)
{
case "LogTime":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime);
break;
case "IP":
break;
case "IP":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.Ip) : sortBuilder.Descending(t => t.Ip);
break;
case "UserName":
break;
case "UserName":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserName) : sortBuilder.Descending(t => t.UserName);
break;
case "City":
break;
case "City":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.City) : sortBuilder.Descending(t => t.City);
break;
case "Browser":
break;
case "Browser":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.Browser) : sortBuilder.Descending(t => t.Browser);
break;
case "OS":
break;
case "OS":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.OS) : sortBuilder.Descending(t => t.OS);
break;
case "RequestUrl":
break;
case "RequestUrl":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.RequestUrl) : sortBuilder.Descending(t => t.RequestUrl);
break;
break;
}
var traces = DbManager.Traces.Find(filter).Sort(sort).ToList();
@ -73,7 +73,16 @@ namespace Bootstrap.DataAccess.MongoDB
{
p.Id = null;
DbManager.Traces.InsertOne(p);
ClearTraces();
return true;
}
private static void ClearTraces()
{
System.Threading.Tasks.Task.Run(() =>
{
DbManager.Traces.DeleteMany(t => t.LogTime < DateTime.Now.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
});
}
}
}

View File

@ -132,30 +132,36 @@ namespace Bootstrap.DataAccess
/// 程序异常时长 默认1月
/// </summary>
/// <returns></returns>
public int RetrieveExceptionsLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().Where(d => d.Category == "系统设置" && d.Name == "程序异常保留时长" && d.Define == 0).FirstOrDefault()?.Code, 1);
public int RetrieveExceptionsLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "程序异常保留时长" && d.Define == 0)?.Code, 1);
/// <summary>
/// 操作日志时长 默认12月
/// </summary>
/// <returns></returns>
public int RetrieveLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().Where(d => d.Category == "系统设置" && d.Name == "操作日志保留时长" && d.Define == 0).FirstOrDefault()?.Code, 12);
public int RetrieveLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "操作日志保留时长" && d.Define == 0)?.Code, 12);
/// <summary>
/// 登录日志时长 默认12月
/// </summary>
/// <returns></returns>
public int RetrieveLoginLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().Where(d => d.Category == "系统设置" && d.Name == "登录日志保留时长" && d.Define == 0).FirstOrDefault()?.Code, 12);
public int RetrieveLoginLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "登录日志保留时长" && d.Define == 0)?.Code, 12);
/// <summary>
/// Cookie保存时长 默认7天
/// </summary>
/// <returns></returns>
public int RetrieveCookieExpiresPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().Where(d => d.Category == "系统设置" && d.Name == "Cookie保留时长" && d.Define == 0).FirstOrDefault()?.Code, 7);
public int RetrieveCookieExpiresPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "Cookie保留时长" && d.Define == 0)?.Code, 7);
/// <summary>
/// 获得 项目是否获取登录地点 默认为false
/// </summary>
/// <returns></returns>
public int RetrieveLocaleIP() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().Where(d => d.Category == "系统设置" && d.Name == "获取IP地点" && d.Define == 0).FirstOrDefault()?.Code, 0);
public int RetrieveLocaleIP() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "获取IP地点" && d.Define == 0)?.Code, 0);
/// <summary>
/// 获得 访问日志保留时长 默认为1个月
/// </summary>
/// <returns></returns>
public int RetrieveAccessLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "访问日志保留时长" && d.Define == 0)?.Code, 1);
}
}

View File

@ -1,4 +1,4 @@
using Bootstrap.Security;
using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic;
@ -145,5 +145,11 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <returns></returns>
public static int RetrieveLocaleIP() => DbContextManager.Create<Dict>().RetrieveLocaleIP();
/// <summary>
/// 访问日志保留时长 默认一个月
/// </summary>
/// <returns></returns>
public static int RetrieveAccessLogPeriod() => DbContextManager.Create<Dict>().RetrieveAccessLogPeriod();
}
}

View File

@ -1,79 +1,85 @@
using Longbow.Web.Mvc;
using PetaPoco;
using System;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
public class Trace
{
/// <summary>
/// 获得/设置 操作日志主键ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 获得/设置 用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 获得/设置 操作时间
/// </summary>
public DateTime LogTime { get; set; }
/// <summary>
/// 获得/设置 客户端IP
/// </summary>
public string Ip { get; set; }
/// <summary>
///
/// </summary>
public string City { get; set; }
/// <summary>
///
/// </summary>
public string Browser { get; set; }
/// <summary>
///
/// </summary>
public string OS { get; set; }
/// <summary>
/// 获取/设置 请求网址
/// </summary>
public string RequestUrl { get; set; }
/// <summary>
/// 保存访问历史记录
/// </summary>
/// <param name="p"></param>
public virtual bool Save(Trace p)
{
if (p == null) throw new ArgumentNullException(nameof(p));
DbManager.Create().Save(p);
return true;
}
/// <summary>
///
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public virtual Page<Trace> Retrieves(PaginationOption po, DateTime? startTime, DateTime? endTime)
{
var sql = new Sql("select * from Traces");
if (startTime.HasValue) sql.Append("where LogTime > @0", startTime.Value);
if (endTime.HasValue) sql.Append("where LogTime < @0", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) sql.Append("where LogTime > @0", DateTime.Today.AddDays(-7));
sql.Append($"order by {po.Sort} {po.Order}");
return DbManager.Create().Page<Trace>(po.PageIndex, po.Limit, sql);
}
}
}
using Longbow.Web.Mvc;
using PetaPoco;
using System;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
public class Trace
{
/// <summary>
/// 获得/设置 操作日志主键ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 获得/设置 用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 获得/设置 操作时间
/// </summary>
public DateTime LogTime { get; set; }
/// <summary>
/// 获得/设置 客户端IP
/// </summary>
public string Ip { get; set; }
/// <summary>
///
/// </summary>
public string City { get; set; }
/// <summary>
///
/// </summary>
public string Browser { get; set; }
/// <summary>
///
/// </summary>
public string OS { get; set; }
/// <summary>
/// 获取/设置 请求网址
/// </summary>
public string RequestUrl { get; set; }
/// <summary>
/// 保存访问历史记录
/// </summary>
/// <param name="p"></param>
public virtual bool Save(Trace p)
{
if (p == null) throw new ArgumentNullException(nameof(p));
DbManager.Create().Save(p);
ClearTraces();
return true;
}
/// <summary>
///
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public virtual Page<Trace> Retrieves(PaginationOption po, DateTime? startTime, DateTime? endTime)
{
var sql = new Sql("select * from Traces");
if (startTime.HasValue) sql.Append("where LogTime > @0", startTime.Value);
if (endTime.HasValue) sql.Append("where LogTime < @0", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) sql.Append("where LogTime > @0", DateTime.Today.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
sql.Append($"order by {po.Sort} {po.Order}");
return DbManager.Create().Page<Trace>(po.PageIndex, po.Limit, sql);
}
private static void ClearTraces() => System.Threading.Tasks.Task.Run(() =>
{
DbManager.Create().Execute("delete from Traces where LogTime < @0", DateTime.Now.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
});
}
}

View File

@ -1,4 +1,4 @@
USE [BootstrapAdmin]
USE [BootstrapAdmin]
GO
DELETE From Users where ID = 1
@ -35,6 +35,7 @@ INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'网站设
INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'程序异常保留时长', '1', 0)
INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'操作日志保留时长', '12', 0)
INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'登录日志保留时长', '12', 0)
INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'访问日志保留时长', '1', 0)
-- 时长单位 天
INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'Cookie保留时长', '7', 0)

View File

@ -313,5 +313,12 @@
"Name": "获取IP地点",
"Code": "0",
"Define": NumberInt(0)
},
{
"_id": ObjectId("5bd6c73d5fa31256f77e4a45"),
"Category": "系统设置",
"Name": "访问日志保留时长",
"Code": "1",
"Define": NumberInt(0)
}
]

View File

@ -32,6 +32,7 @@ INSERT INTO Dicts (ID, Category, Name, Code, Define) VALUES (NULL, '网站设置
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '程序异常保留时长', '1', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '操作日志保留时长', '12', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '登录日志保留时长', '12', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '访问日志保留时长', '1', 0);
-- 时长单位 天
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'Cookie保留时长', '7', 0);

View File

@ -32,6 +32,7 @@ INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('网站设置', '前台
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '程序异常保留时长', '1', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '操作日志保留时长', '12', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '登录日志保留时长', '12', 0);
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '访问日志保留时长', '1', 0);
-- 时长单位 天
INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'Cookie保留时长', '7', 0);

View File

@ -1,4 +1,4 @@
DELETE From Users where ID = 1;
DELETE From Users where ID = 1;
-- ADMIN/123789
INSERT INTO Users (ID, UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedTime,ApprovedBy, [Description]) values (1, 'Admin', 'Es7WVgNsJuELwWK8daCqufUBknCsSC0IYDphQZAiGOo=', 'W5vpBEOYRGHkQXatN0t+ECM/U8cHDuEgrq56+zZBk4J481xH', 'Administrator', datetime(CURRENT_TIMESTAMP, 'localtime'), datetime(CURRENT_TIMESTAMP, 'localtime'), 'system', '系统默认创建');
@ -30,6 +30,7 @@ INSERT INTO [Dicts] ([ID], [Category], [Name], [Code], [Define]) VALUES (NULL, '
INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '程序异常保留时长', '1', 0);
INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '操作日志保留时长', '12', 0);
INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '登录日志保留时长', '12', 0);
INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '访问日志保留时长', '1', 0);
-- 时长单位 天
INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', 'Cookie保留时长', '7', 0);

View File

@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Xunit;
namespace Bootstrap.DataAccess
@ -131,5 +131,12 @@ namespace Bootstrap.DataAccess
var dict = new Dict();
Assert.Equal(0, dict.RetrieveLocaleIP());
}
[Fact]
public void RetrieveAccessLogPeriod_Ok()
{
var dict = new Dict();
Assert.Equal(1, dict.RetrieveAccessLogPeriod());
}
}
}