From 45cbc0eb5337a7ece3fc9d4b1fd306e8c428ec8e Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Tue, 30 Oct 2018 22:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8Exception=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=88=B0ExceptionHelper=E7=B1=BB=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess.MongoDB/Exceptions.cs | 10 ++++++++-- Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs | 10 ++++++++++ Bootstrap.DataAccess.SQLite/Exceptions.cs | 5 ++--- Bootstrap.DataAccess/Exceptions.cs | 7 +++---- Bootstrap.DataAccess/Helper/ExceptionsHelper.cs | 7 ++++++- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Bootstrap.DataAccess.MongoDB/Exceptions.cs b/Bootstrap.DataAccess.MongoDB/Exceptions.cs index 0efa19dd..6cfbf64f 100644 --- a/Bootstrap.DataAccess.MongoDB/Exceptions.cs +++ b/Bootstrap.DataAccess.MongoDB/Exceptions.cs @@ -9,14 +9,20 @@ namespace Bootstrap.DataAccess.MongoDB /// public class Exceptions : DataAccess.Exceptions { + private static void ClearExceptions() + { + System.Threading.Tasks.Task.Run(() => + { + MongoDbAccessManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddDays(-7)); + }); + } /// /// /// /// public override IEnumerable RetrieveExceptions() { - var msg = MongoDbAccessManager.DBAccess.GetCollection("Exceptions"); - return msg.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList(); + return MongoDbAccessManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList(); } } } diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index 04b136f6..7bd270f4 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -49,6 +49,16 @@ namespace Bootstrap.DataAccess.MongoDB /// /// /// + public static IMongoCollection Exceptions + { + get + { + return DBAccess.GetCollection("Exceptions"); + } + } + /// + /// + /// public static IMongoCollection Users { get diff --git a/Bootstrap.DataAccess.SQLite/Exceptions.cs b/Bootstrap.DataAccess.SQLite/Exceptions.cs index 92208f00..ffeab6fe 100644 --- a/Bootstrap.DataAccess.SQLite/Exceptions.cs +++ b/Bootstrap.DataAccess.SQLite/Exceptions.cs @@ -1,5 +1,4 @@ using Longbow; -using Longbow.Cache; using Longbow.Configuration; using Longbow.Data; using System; @@ -33,7 +32,7 @@ namespace Bootstrap.DataAccess.SQLite /// /// /// - public override void Log(Exception ex, NameValueCollection additionalInfo) + public override bool Log(Exception ex, NameValueCollection additionalInfo) { if (additionalInfo == null) { @@ -56,9 +55,9 @@ namespace Bootstrap.DataAccess.SQLite cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace))); DbAccessManager.DBAccess.ExecuteNonQuery(cmd); - CacheManager.Clear(ExceptionsHelper.RetrieveExceptionsDataKey); ClearExceptions(); } + return true; } /// /// 查询一周内所有异常 diff --git a/Bootstrap.DataAccess/Exceptions.cs b/Bootstrap.DataAccess/Exceptions.cs index b43703df..fa377050 100644 --- a/Bootstrap.DataAccess/Exceptions.cs +++ b/Bootstrap.DataAccess/Exceptions.cs @@ -1,5 +1,4 @@ -using Longbow.Cache; -using Longbow.Configuration; +using Longbow.Configuration; using Longbow.Data; using System; using System.Collections.Generic; @@ -70,7 +69,7 @@ namespace Bootstrap.DataAccess /// /// /// - public virtual void Log(Exception ex, NameValueCollection additionalInfo) + public virtual bool Log(Exception ex, NameValueCollection additionalInfo) { if (additionalInfo == null) { @@ -93,9 +92,9 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace))); DbAccessManager.DBAccess.ExecuteNonQuery(cmd); - CacheManager.Clear(ExceptionsHelper.RetrieveExceptionsDataKey); ClearExceptions(); } + return true; } /// /// 查询一周内所有异常 diff --git a/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs b/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs index 85e7ec3e..833e2da2 100644 --- a/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs +++ b/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs @@ -21,7 +21,12 @@ namespace Bootstrap.DataAccess /// /// /// - public static void Log(Exception ex, NameValueCollection additionalInfo) => DbAdapterManager.Create().Log(ex, additionalInfo); + public static void Log(Exception ex, NameValueCollection additionalInfo) + { + var ret = DbAdapterManager.Create().Log(ex, additionalInfo); + if (ret) CacheManager.Clear(RetrieveExceptionsDataKey); + } + /// /// 查询一周内所有异常 ///