重构代码:移动Exception缓存处理到ExceptionHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-30 22:15:29 +08:00
parent b65676166e
commit 45cbc0eb53
5 changed files with 29 additions and 10 deletions

View File

@ -9,14 +9,20 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary>
public class Exceptions : DataAccess.Exceptions
{
private static void ClearExceptions()
{
System.Threading.Tasks.Task.Run(() =>
{
MongoDbAccessManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddDays(-7));
});
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.Exceptions> RetrieveExceptions()
{
var msg = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Exceptions>("Exceptions");
return msg.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList();
return MongoDbAccessManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList();
}
}
}

View File

@ -49,6 +49,16 @@ namespace Bootstrap.DataAccess.MongoDB
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.Exceptions> Exceptions
{
get
{
return DBAccess.GetCollection<DataAccess.Exceptions>("Exceptions");
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<User> Users
{
get

View File

@ -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
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 查询一周内所有异常

View File

@ -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
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 查询一周内所有异常

View File

@ -21,7 +21,12 @@ namespace Bootstrap.DataAccess
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
public static void Log(Exception ex, NameValueCollection additionalInfo) => DbAdapterManager.Create<Exceptions>().Log(ex, additionalInfo);
public static void Log(Exception ex, NameValueCollection additionalInfo)
{
var ret = DbAdapterManager.Create<Exceptions>().Log(ex, additionalInfo);
if (ret) CacheManager.Clear(RetrieveExceptionsDataKey);
}
/// <summary>
/// 查询一周内所有异常
/// </summary>