2018-06-07 00:45:47 +08:00
|
|
|
|
using Longbow.Cache;
|
2018-09-08 19:37:42 +08:00
|
|
|
|
using Longbow.Configuration;
|
2016-11-11 21:44:41 +08:00
|
|
|
|
using Longbow.Data;
|
2018-06-12 13:46:06 +08:00
|
|
|
|
using Longbow.Web.WebSockets;
|
2018-06-15 16:14:49 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2016-11-10 16:39:07 +08:00
|
|
|
|
using System;
|
2016-11-11 21:44:41 +08:00
|
|
|
|
using System.Collections.Generic;
|
2016-11-10 16:39:07 +08:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2018-06-15 16:14:49 +08:00
|
|
|
|
using System.Text;
|
2016-11-10 16:39:07 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-11-10 16:39:07 +08:00
|
|
|
|
public static class ExceptionHelper
|
|
|
|
|
{
|
2017-04-06 13:28:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static readonly string RetrieveExceptionsDataKey = "ExceptionHelper-RetrieveExceptions";
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
/// <param name="additionalInfo"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-06-12 11:17:21 +08:00
|
|
|
|
public static void Log(Exception ex, NameValueCollection additionalInfo)
|
2016-11-10 16:39:07 +08:00
|
|
|
|
{
|
2018-09-08 22:47:24 +08:00
|
|
|
|
var errorPage = additionalInfo["ErrorPage"] ?? (ex.Message.Length > 50 ? ex.Message.Substring(0, 50) : ex.Message);
|
2018-07-19 18:12:04 +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.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
2016-11-10 16:39:07 +08:00
|
|
|
|
{
|
2018-07-19 18:12:04 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@AppDomainName", AppDomain.CurrentDomain.FriendlyName));
|
2018-09-08 17:20:12 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ErrorPage", errorPage));
|
2018-07-19 18:12:04 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", DBAccessFactory.ToDBValue(additionalInfo["UserId"])));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserIp", DBAccessFactory.ToDBValue(additionalInfo["UserIp"])));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ExceptionType", ex.GetType().FullName));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Message", ex.Message));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@StackTrace", DBAccessFactory.ToDBValue(ex.StackTrace)));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
CacheManager.Clear(RetrieveExceptionsDataKey);
|
2018-09-08 17:20:12 +08:00
|
|
|
|
var category = "App";
|
|
|
|
|
if (ex.GetType().IsSubclassOf(typeof(DbException))) category = "DB";
|
|
|
|
|
WebSocketServerManager.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new MessageBody[] { new MessageBody() { Category = category, Message = ex.Message } }))));
|
2018-09-08 19:37:42 +08:00
|
|
|
|
ClearExceptionsAsync();
|
2016-11-10 16:39:07 +08:00
|
|
|
|
}
|
2016-11-11 21:44:41 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2018-09-08 19:37:42 +08:00
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void ClearExceptionsAsync()
|
|
|
|
|
{
|
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
string sql = $"delete from Exceptions where LogTime < DATEADD(MONTH, -{ConfigurationManager.AppSettings["KeepExceptionsPeriod"]}, GETDATE())";
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询一周内所有异常
|
2016-11-11 21:44:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Exceptions> RetrieveExceptions()
|
|
|
|
|
{
|
2017-04-05 11:54:21 +08:00
|
|
|
|
return CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key =>
|
2016-11-11 21:44:41 +08:00
|
|
|
|
{
|
2018-09-08 19:37:42 +08:00
|
|
|
|
string sql = "select * from Exceptions where DATEDIFF(Week, LogTime, GETDATE()) = 0 order by LogTime desc";
|
2017-03-30 16:15:45 +08:00
|
|
|
|
List<Exceptions> exceptions = new List<Exceptions>();
|
2016-11-11 21:44:41 +08:00
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
2018-09-08 19:37:42 +08:00
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
2016-11-11 21:44:41 +08:00
|
|
|
|
{
|
2018-09-08 19:37:42 +08:00
|
|
|
|
while (reader.Read())
|
2016-11-11 21:44:41 +08:00
|
|
|
|
{
|
2018-09-08 19:37:42 +08:00
|
|
|
|
exceptions.Add(new Exceptions()
|
2016-11-11 21:44:41 +08:00
|
|
|
|
{
|
2018-09-08 19:37:42 +08:00
|
|
|
|
Id = (int)reader[0],
|
|
|
|
|
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],
|
|
|
|
|
StackTrace = (string)reader[7],
|
|
|
|
|
LogTime = (DateTime)reader[8],
|
|
|
|
|
});
|
2016-11-11 21:44:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-30 16:15:45 +08:00
|
|
|
|
return exceptions;
|
2017-04-05 11:54:21 +08:00
|
|
|
|
});
|
2016-11-10 16:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|