2018-10-30 22:15:29 +08:00
|
|
|
|
using Longbow.Configuration;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using Longbow.Data;
|
|
|
|
|
using System;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
|
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; }
|
2016-11-18 10:18:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AppDomainName { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ErrorPage { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2017-03-30 16:15:45 +08:00
|
|
|
|
public string UserId { get; set; }
|
2016-11-18 10:18:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string UserIp { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Message { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string StackTrace { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTime LogTime { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ExceptionType { 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
|
|
|
|
|
|
|
|
|
private static void ClearExceptions()
|
|
|
|
|
{
|
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
string sql = $"delete from Exceptions where LogTime < DATEADD(MONTH, -{ConfigurationManager.AppSettings["KeepExceptionsPeriod"]}, GETDATE())";
|
|
|
|
|
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
});
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2018-11-02 14:43:36 +08:00
|
|
|
|
if (ex == null) return true;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
if (additionalInfo == null)
|
|
|
|
|
{
|
|
|
|
|
additionalInfo = new NameValueCollection
|
|
|
|
|
{
|
|
|
|
|
["UserId"] = null,
|
|
|
|
|
["UserIp"] = null,
|
|
|
|
|
["ErrorPage"] = null
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-10-23 13:50:21 +08:00
|
|
|
|
var errorPage = additionalInfo["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name);
|
2018-10-21 10:02:59 +08:00
|
|
|
|
var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, GetDate())";
|
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@AppDomainName", AppDomain.CurrentDomain.FriendlyName));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ErrorPage", errorPage));
|
2018-10-21 21:08:35 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", DbAdapterManager.ToDBValue(additionalInfo["UserId"])));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserIp", DbAdapterManager.ToDBValue(additionalInfo["UserIp"])));
|
2018-10-21 10:02:59 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ExceptionType", ex.GetType().FullName));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message));
|
2018-10-21 21:08:35 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace)));
|
2018-10-21 10:02:59 +08:00
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
ClearExceptions();
|
|
|
|
|
}
|
2018-10-30 22:15:29 +08:00
|
|
|
|
return true;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询一周内所有异常
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2018-10-21 10:02:59 +08:00
|
|
|
|
public virtual IEnumerable<Exceptions> RetrieveExceptions()
|
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
string sql = "select * from Exceptions where DATEDIFF(Week, LogTime, GETDATE()) = 0 order by LogTime desc";
|
|
|
|
|
List<Exceptions> exceptions = new List<Exceptions>();
|
|
|
|
|
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
while (reader.Read())
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
exceptions.Add(new Exceptions()
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-30 13:07:29 +08:00
|
|
|
|
Id = reader[0].ToString(),
|
2018-10-28 15:03:59 +08:00
|
|
|
|
AppDomainName = (string)reader[1],
|
|
|
|
|
ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
|
|
|
|
|
UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3],
|
|
|
|
|
UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4],
|
|
|
|
|
ExceptionType = (string)reader[5],
|
|
|
|
|
Message = (string)reader[6],
|
2018-11-02 14:43:36 +08:00
|
|
|
|
StackTrace = reader.IsDBNull(7) ? string.Empty : (string)reader[7],
|
2018-10-28 15:03:59 +08:00
|
|
|
|
LogTime = (DateTime)reader[8],
|
|
|
|
|
});
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
2018-10-28 15:03:59 +08:00
|
|
|
|
}
|
|
|
|
|
return exceptions;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
2016-11-18 10:18:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|