2018-11-02 18:31:32 +08:00
using Longbow ;
using Longbow.Configuration ;
2019-01-11 23:20:28 +08:00
using Longbow.Web.Mvc ;
using PetaPoco ;
2018-10-21 10:02:59 +08:00
using System ;
2018-10-19 23:09:52 +08:00
using System.Collections.Generic ;
using System.Collections.Specialized ;
2016-11-18 10:18:41 +08:00
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
public class Exceptions
{
/// <summary>
///
/// </summary>
2018-10-30 13:07:29 +08:00
public string Id { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
public string AppDomainName { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
public string ErrorPage { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
2017-03-30 16:15:45 +08:00
public string UserId { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
public string UserIp { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
2019-01-11 23:20:28 +08:00
public string ExceptionType { get ; set ; }
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
2019-01-11 23:20:28 +08:00
public string Message { get ; set ; }
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
2019-01-11 23:20:28 +08:00
public string StackTrace { get ; set ; }
2016-11-18 10:18:41 +08:00
/// <summary>
///
/// </summary>
2019-01-11 23:20:28 +08:00
public DateTime LogTime { get ; set ; }
2016-11-18 11:25:32 +08:00
/// <summary>
/// 获得/设置 时间描述 2分钟内为刚刚
/// </summary>
public string Period { get ; set ; }
2018-10-21 10:02:59 +08:00
2019-01-11 23:20:28 +08:00
private static void ClearExceptions ( ) = > System . Threading . Tasks . Task . Run ( ( ) = >
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
DbManager . Db . Execute ( "delete from Exceptions where LogTime < @0" , DateTime . Now . AddMonths ( 0 - LgbConvert . ReadValue ( ConfigurationManager . AppSettings [ "KeepExceptionsPeriod" ] , 1 ) ) ) ;
} ) ;
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
2018-10-30 22:15:29 +08:00
public virtual bool Log ( Exception ex , NameValueCollection additionalInfo )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
if ( ex = = null ) throw new ArgumentNullException ( nameof ( ex ) ) ;
var errorPage = additionalInfo ? [ "ErrorPage" ] ? ? ( ex . GetType ( ) . Name . Length > 50 ? ex . GetType ( ) . Name . Substring ( 0 , 50 ) : ex . GetType ( ) . Name ) ;
DbManager . Db . Insert ( new Exceptions ( )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ( ) ;
2018-10-30 22:15:29 +08:00
return true ;
2018-10-21 10:02:59 +08:00
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询一周内所有异常
/// </summary>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < Exceptions > Retrieves ( ) = > DbManager . Db . Fetch < Exceptions > ( "select * from Exceptions where LogTime > @0 order by LogTime desc" , DateTime . Now . AddDays ( - 7 ) ) ;
/// <summary>
///
/// </summary>
/// <param name="po"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="sort"></param>
/// <returns></returns>
public virtual Page < Exceptions > RetrievePages ( PaginationOption po , DateTime ? startTime , DateTime ? endTime )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ) ;
if ( startTime = = null & & endTime = = null ) sql . Append ( "where LogTime > @0" , DateTime . Today . AddDays ( - 7 ) ) ;
sql . Append ( "order by @0" , $"{po.Sort} {po.Order}" ) ;
return DbManager . Db . Page < Exceptions > ( po . PageIndex , po . Limit , sql ) ;
2018-10-21 10:02:59 +08:00
}
2016-11-18 10:18:41 +08:00
}
}