From 72729e577cb713b1c2382e24753a4704197d6112 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 18 Mar 2019 17:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=EF=BC=9A=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=BC=82=E5=B8=B8=E5=90=8E=E5=8F=B0=E6=B8=85=E7=90=86?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E6=97=A5=E5=BF=97=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=BC=82=E5=B8=B8=E4=BF=9D=E7=95=99=E6=97=B6?= =?UTF-8?q?=E9=95=BF=E6=9C=AA=E7=94=9F=E6=95=88=20closed=20#ITUOK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue https://gitee.com/LongbowEnterprise/dashboard/issues?id=ITUOK --- Bootstrap.DataAccess.MongoDB/Exceptions.cs | 8 +- Bootstrap.DataAccess/Exceptions.cs | 238 ++++++++++----------- Bootstrap.DataAccess/Helper/DictHelper.cs | 2 +- 3 files changed, 124 insertions(+), 124 deletions(-) diff --git a/Bootstrap.DataAccess.MongoDB/Exceptions.cs b/Bootstrap.DataAccess.MongoDB/Exceptions.cs index 12f1f140..d43c7955 100644 --- a/Bootstrap.DataAccess.MongoDB/Exceptions.cs +++ b/Bootstrap.DataAccess.MongoDB/Exceptions.cs @@ -1,4 +1,4 @@ -using Longbow.Web.Mvc; +using Longbow.Web.Mvc; using MongoDB.Driver; using PetaPoco; using System; @@ -17,7 +17,7 @@ namespace Bootstrap.DataAccess.MongoDB { System.Threading.Tasks.Task.Run(() => { - DbManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddDays(-7)); + DbManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())); }); } @@ -27,7 +27,7 @@ namespace Bootstrap.DataAccess.MongoDB /// public override IEnumerable Retrieves() { - return DbManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList(); + return DbManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())).ToList(); } /// @@ -71,7 +71,7 @@ namespace Bootstrap.DataAccess.MongoDB 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)); + if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())); // sort var sortBuilder = Builders.Sort; diff --git a/Bootstrap.DataAccess/Exceptions.cs b/Bootstrap.DataAccess/Exceptions.cs index efdad139..bd07f250 100644 --- a/Bootstrap.DataAccess/Exceptions.cs +++ b/Bootstrap.DataAccess/Exceptions.cs @@ -1,119 +1,119 @@ -using Longbow.Web.Mvc; -using PetaPoco; -using System; -using System.Collections.Generic; -using System.Collections.Specialized; - -namespace Bootstrap.DataAccess -{ - /// - /// - /// - public class Exceptions - { - /// - /// - /// - public string Id { get; set; } - - /// - /// - /// - public string AppDomainName { get; set; } - - /// - /// - /// - public string ErrorPage { get; set; } - - /// - /// - /// - public string UserId { get; set; } - - /// - /// - /// - public string UserIp { get; set; } - - /// - /// - /// - public string ExceptionType { get; set; } - - /// - /// - /// - public string Message { get; set; } - - /// - /// - /// - public string StackTrace { get; set; } - - /// - /// - /// - public DateTime LogTime { get; set; } - - /// - /// 获得/设置 时间描述 2分钟内为刚刚 - /// - public string Period { get; set; } - - private static void ClearExceptions() => System.Threading.Tasks.Task.Run(() => - { - DbManager.Create().Execute("delete from Exceptions where LogTime < @0", DateTime.Now.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())); - }); - - /// - /// - /// - /// - /// - /// - public virtual bool Log(Exception ex, NameValueCollection additionalInfo) - { - if (ex == null) return true; - - var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name); - DbManager.Create().Insert(new Exceptions - { - AppDomainName = AppDomain.CurrentDomain.FriendlyName, - ErrorPage = errorPage, - UserId = additionalInfo?["UserId"], - UserIp = additionalInfo?["UserIp"], - ExceptionType = ex.GetType().FullName, - Message = ex.Message, - StackTrace = ex.StackTrace, - LogTime = DateTime.Now - }); - ClearExceptions(); - return true; - } - - /// - /// 查询一周内所有异常 - /// - /// - public virtual IEnumerable Retrieves() => DbManager.Create().Fetch("select * from Exceptions where LogTime > @0 order by LogTime desc", DateTime.Now.AddDays(-7)); - - /// - /// - /// - /// - /// - /// - /// - public virtual Page RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) - { - var sql = new Sql("select * from Exceptions"); - 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(po.PageIndex, po.Limit, sql); - } - } -} +using Longbow.Web.Mvc; +using PetaPoco; +using System; +using System.Collections.Generic; +using System.Collections.Specialized; + +namespace Bootstrap.DataAccess +{ + /// + /// + /// + public class Exceptions + { + /// + /// + /// + public string Id { get; set; } + + /// + /// + /// + public string AppDomainName { get; set; } + + /// + /// + /// + public string ErrorPage { get; set; } + + /// + /// + /// + public string UserId { get; set; } + + /// + /// + /// + public string UserIp { get; set; } + + /// + /// + /// + public string ExceptionType { get; set; } + + /// + /// + /// + public string Message { get; set; } + + /// + /// + /// + public string StackTrace { get; set; } + + /// + /// + /// + public DateTime LogTime { get; set; } + + /// + /// 获得/设置 时间描述 2分钟内为刚刚 + /// + public string Period { get; set; } + + private static void ClearExceptions() => System.Threading.Tasks.Task.Run(() => + { + DbManager.Create().Execute("delete from Exceptions where LogTime < @0", DateTime.Now.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())); + }); + + /// + /// + /// + /// + /// + /// + public virtual bool Log(Exception ex, NameValueCollection additionalInfo) + { + if (ex == null) return true; + + var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name); + DbManager.Create().Insert(new Exceptions + { + AppDomainName = AppDomain.CurrentDomain.FriendlyName, + ErrorPage = errorPage, + UserId = additionalInfo?["UserId"], + UserIp = additionalInfo?["UserIp"], + ExceptionType = ex.GetType().FullName, + Message = ex.Message, + StackTrace = ex.StackTrace, + LogTime = DateTime.Now + }); + ClearExceptions(); + return true; + } + + /// + /// 查询一周内所有异常 + /// + /// + public virtual IEnumerable Retrieves() => DbManager.Create().Fetch("select * from Exceptions where LogTime > @0 order by LogTime desc", DateTime.Now.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())); + + /// + /// + /// + /// + /// + /// + /// + public virtual Page RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) + { + var sql = new Sql("select * from Exceptions"); + 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.RetrieveExceptionsLogPeriod())); + sql.Append($"order by {po.Sort} {po.Order}"); + + return DbManager.Create().Page(po.PageIndex, po.Limit, sql); + } + } +} diff --git a/Bootstrap.DataAccess/Helper/DictHelper.cs b/Bootstrap.DataAccess/Helper/DictHelper.cs index fd4325ce..0a3086c1 100644 --- a/Bootstrap.DataAccess/Helper/DictHelper.cs +++ b/Bootstrap.DataAccess/Helper/DictHelper.cs @@ -117,7 +117,7 @@ namespace Bootstrap.DataAccess public static IEnumerable> RetrieveApps() => DbContextManager.Create().RetrieveApps(); /// - /// + /// 程序异常时长 默认1月 /// /// public static int RetrieveExceptionsLogPeriod() => DbContextManager.Create().RetrieveExceptionsLogPeriod();