增加功能:异常记录数据库增加自动清理功能,增加配置项KeepExceptionsPeriod,生产环境保存12个月,开发环境保存1个月,界面显示一周内异常数据

This commit is contained in:
Argo-MacBookPro 2018-09-08 19:37:42 +08:00
parent 3ca7196c82
commit e556556e7c
3 changed files with 31 additions and 20 deletions

View File

@ -21,6 +21,7 @@
"KeyPath": "keys", "KeyPath": "keys",
"DisableAutomaticKeyGeneration": false, "DisableAutomaticKeyGeneration": false,
"AllowOrigins": "http://localhost:49823", "AllowOrigins": "http://localhost:49823",
"KeepExceptionsPeriod": 1,
"LongbowCache": { "LongbowCache": {
"CorsItems": [ "CorsItems": [
{ {

View File

@ -20,6 +20,7 @@
"KeyPath": "D:\\App\\Web-App\\keys", "KeyPath": "D:\\App\\Web-App\\keys",
"DisableAutomaticKeyGeneration": true, "DisableAutomaticKeyGeneration": true,
"AllowOrigins": "http://localhost,http://10.15.63.218", "AllowOrigins": "http://localhost,http://10.15.63.218",
"KeepExceptionsPeriod": 12,
"LongbowCache": { "LongbowCache": {
"Enabled": true, "Enabled": true,
"CorsItems": [ "CorsItems": [

View File

@ -1,6 +1,6 @@
using Longbow.Cache; using Longbow.Cache;
using Longbow.Configuration;
using Longbow.Data; using Longbow.Data;
using Longbow.Logging;
using Longbow.Web.WebSockets; using Longbow.Web.WebSockets;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
@ -45,41 +45,50 @@ namespace Bootstrap.DataAccess
var category = "App"; var category = "App";
if (ex.GetType().IsSubclassOf(typeof(DbException))) category = "DB"; 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 } })))); WebSocketServerManager.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new MessageBody[] { new MessageBody() { Category = category, Message = ex.Message } }))));
ClearExceptionsAsync();
} }
} }
/// <summary> /// <summary>
/// 查询所有异常 ///
/// </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>
/// 查询一周内所有异常
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Exceptions> RetrieveExceptions() public static IEnumerable<Exceptions> RetrieveExceptions()
{ {
return CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key => return CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key =>
{ {
string sql = "select top 1000 * from Exceptions order by LogTime desc"; string sql = "select * from Exceptions where DATEDIFF(Week, LogTime, GETDATE()) = 0 order by LogTime desc";
List<Exceptions> exceptions = new List<Exceptions>(); List<Exceptions> exceptions = new List<Exceptions>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) while (reader.Read())
{ {
while (reader.Read()) exceptions.Add(new Exceptions()
{ {
exceptions.Add(new Exceptions() Id = (int)reader[0],
{ AppDomainName = (string)reader[1],
Id = (int)reader[0], ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
AppDomainName = (string)reader[1], UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3],
ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2], UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4],
UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3], ExceptionType = (string)reader[5],
UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4], Message = (string)reader[6],
ExceptionType = (string)reader[5], StackTrace = (string)reader[7],
Message = (string)reader[6], LogTime = (DateTime)reader[8],
StackTrace = (string)reader[7], });
LogTime = (DateTime)reader[8],
});
}
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); }
return exceptions; return exceptions;
}); });
} }