2019-03-18 17:49:28 +08:00
|
|
|
using Longbow.Web.Mvc;
|
2019-02-22 12:19:15 +08:00
|
|
|
using MongoDB.Driver;
|
|
|
|
using PetaPoco;
|
2018-10-30 13:07:29 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-10-31 17:40:40 +08:00
|
|
|
using System.Collections.Specialized;
|
2019-02-22 12:19:15 +08:00
|
|
|
using System.Linq;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public class Exceptions : DataAccess.Exceptions
|
|
|
|
{
|
2018-10-30 22:15:29 +08:00
|
|
|
private static void ClearExceptions()
|
|
|
|
{
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
{
|
2019-03-18 17:49:28 +08:00
|
|
|
DbManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod()));
|
2018-10-30 22:15:29 +08:00
|
|
|
});
|
|
|
|
}
|
2018-10-31 17:40:40 +08:00
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
public override IEnumerable<DataAccess.Exceptions> Retrieves()
|
2018-10-30 13:07:29 +08:00
|
|
|
{
|
2019-03-18 17:49:28 +08:00
|
|
|
return DbManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod())).ToList();
|
2018-10-30 13:07:29 +08:00
|
|
|
}
|
2018-10-31 17:40:40 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
/// <param name="additionalInfo"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override bool Log(Exception ex, NameValueCollection additionalInfo)
|
|
|
|
{
|
2019-02-25 16:02:24 +08:00
|
|
|
if (ex == null) return true;
|
|
|
|
|
2018-10-31 21:29:42 +08:00
|
|
|
var excep = new DataAccess.Exceptions
|
|
|
|
{
|
|
|
|
Id = null,
|
|
|
|
AppDomainName = AppDomain.CurrentDomain.FriendlyName,
|
2018-11-01 10:44:19 +08:00
|
|
|
ErrorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name),
|
2018-10-31 21:29:42 +08:00
|
|
|
ExceptionType = ex.GetType().FullName,
|
|
|
|
LogTime = DateTime.Now,
|
|
|
|
Message = ex.Message,
|
|
|
|
StackTrace = ex.StackTrace,
|
|
|
|
UserId = additionalInfo?["UserId"],
|
|
|
|
UserIp = additionalInfo?["UserIp"]
|
|
|
|
};
|
2019-01-15 12:52:09 +08:00
|
|
|
DbManager.Exceptions.InsertOne(excep);
|
2018-10-31 17:40:40 +08:00
|
|
|
ClearExceptions();
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-22 12:19:15 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="po"></param>
|
|
|
|
/// <param name="startTime"></param>
|
|
|
|
/// <param name="endTime"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override Page<DataAccess.Exceptions> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime)
|
|
|
|
{
|
2019-03-17 16:18:09 +08:00
|
|
|
// filter
|
|
|
|
var filterBuilder = Builders<DataAccess.Exceptions>.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));
|
2019-03-18 17:49:28 +08:00
|
|
|
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod()));
|
2019-02-22 16:27:39 +08:00
|
|
|
|
2019-03-17 16:18:09 +08:00
|
|
|
// sort
|
|
|
|
var sortBuilder = Builders<DataAccess.Exceptions>.Sort;
|
|
|
|
SortDefinition<DataAccess.Exceptions> sort = null;
|
2019-02-22 16:27:39 +08:00
|
|
|
switch (po.Sort)
|
|
|
|
{
|
2019-03-17 16:18:09 +08:00
|
|
|
case "LogTime":
|
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime);
|
|
|
|
break;
|
2019-02-22 16:27:39 +08:00
|
|
|
case "ErrorPage":
|
2019-03-17 16:18:09 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.ErrorPage) : sortBuilder.Descending(t => t.ErrorPage);
|
2019-02-22 16:27:39 +08:00
|
|
|
break;
|
|
|
|
case "UserId":
|
2019-03-17 16:18:09 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserId) : sortBuilder.Descending(t => t.UserId);
|
2019-02-22 16:27:39 +08:00
|
|
|
break;
|
|
|
|
case "UserIp":
|
2019-03-17 16:18:09 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserIp) : sortBuilder.Descending(t => t.UserIp);
|
2019-02-22 16:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-03-17 16:18:09 +08:00
|
|
|
|
|
|
|
var exceps = DbManager.Exceptions.Find(filter).Sort(sort).ToList();
|
2019-02-22 12:19:15 +08:00
|
|
|
return new Page<DataAccess.Exceptions>()
|
|
|
|
{
|
|
|
|
Context = exceps,
|
|
|
|
CurrentPage = po.PageIndex,
|
|
|
|
ItemsPerPage = po.Limit,
|
|
|
|
TotalItems = exceps.Count,
|
|
|
|
TotalPages = (long)Math.Ceiling(exceps.Count * 1.0 / po.Limit),
|
|
|
|
Items = exceps.Skip(po.Offset).Take(po.Limit).ToList()
|
|
|
|
};
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
}
|
|
|
|
}
|