2016-11-11 21:44:41 +08:00
|
|
|
|
using Longbow.Caching;
|
|
|
|
|
using Longbow.Data;
|
|
|
|
|
using Longbow.ExceptionManagement;
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2016-11-11 21:44:41 +08:00
|
|
|
|
internal const string RetrieveExceptionsDataKey = "ExceptionHelper-RetrieveExceptions";
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
/// <param name="additionalInfo"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-11-11 21:44:41 +08:00
|
|
|
|
public static bool Log(Exception ex, NameValueCollection additionalInfo)
|
2016-11-10 16:39:07 +08:00
|
|
|
|
{
|
2016-11-11 21:44:41 +08:00
|
|
|
|
bool ret = false;
|
2016-11-10 16:39:07 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-18 10:18:41 +08:00
|
|
|
|
var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, GetDate())";
|
2016-11-10 16:39:07 +08:00
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@AppDomainName", AppDomain.CurrentDomain.FriendlyName));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ErrorPage", additionalInfo["ErrorPage"]));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", DBAccess.ToDBValue(additionalInfo["UserId"])));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserIp", additionalInfo["UserIp"]));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ExceptionType", ex.GetType().FullName));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Message", ex.Message));
|
2016-11-10 16:39:07 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@StackTrace", DBAccess.ToDBValue(ex.StackTrace), ParameterDirection.Input));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2016-11-11 21:44:41 +08:00
|
|
|
|
ret = true;
|
2016-11-10 16:39:07 +08:00
|
|
|
|
}
|
2016-11-18 10:18:41 +08:00
|
|
|
|
catch (Exception e)
|
2016-11-10 16:39:07 +08:00
|
|
|
|
{
|
2016-11-18 10:18:41 +08:00
|
|
|
|
throw new Exception("Excetion Log Error", e);
|
2016-11-10 16:39:07 +08:00
|
|
|
|
}
|
2016-11-11 21:44:41 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有异常
|
|
|
|
|
/// </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
|
|
|
|
{
|
2016-12-16 10:52:13 +08:00
|
|
|
|
string sql = "select top 1000 * from Exceptions 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);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
exceptions.Add(new Exceptions()
|
2016-11-11 21:44:41 +08:00
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
Id = (int)reader[0],
|
2016-11-11 21:44:41 +08:00
|
|
|
|
AppDomainName = (string)reader[1],
|
|
|
|
|
ErrorPage = (string)reader[2],
|
2017-03-30 16:15:45 +08:00
|
|
|
|
UserId = (string)reader[3],
|
2016-11-11 21:44:41 +08:00
|
|
|
|
UserIp = (string)reader[4],
|
2016-11-18 14:42:51 +08:00
|
|
|
|
ExceptionType = (string)reader[5],
|
|
|
|
|
Message = (string)reader[6],
|
|
|
|
|
StackTrace = (string)reader[7],
|
|
|
|
|
LogTime = (DateTime)reader[8],
|
2016-11-11 21:44:41 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|