feat: 增加异常数据库日志功能
This commit is contained in:
parent
3c658dba46
commit
eab30a9a6d
|
@ -0,0 +1,64 @@
|
|||
using System.ComponentModel;
|
||||
|
||||
namespace BootstrapAdmin.DataAccess.Models;
|
||||
|
||||
/// <summary>
|
||||
/// 异常实体类
|
||||
/// </summary>
|
||||
public class Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// 获得/设置 主键
|
||||
/// </summary>
|
||||
public string? Id { get; set; }
|
||||
|
||||
[DisplayName("应用程序")]
|
||||
public string? AppDomainName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 用户请求页面地址
|
||||
/// </summary>
|
||||
[DisplayName("请求网址")]
|
||||
public string? ErrorPage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 用户 ID
|
||||
/// </summary>
|
||||
[DisplayName("用户名")]
|
||||
public string? UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 用户 IP
|
||||
/// </summary>
|
||||
[DisplayName("登录主机")]
|
||||
public string? UserIp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 异常类型
|
||||
/// </summary>
|
||||
[DisplayName("异常类型")]
|
||||
public string? ExceptionType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 异常错误描述信息
|
||||
/// </summary>
|
||||
[DisplayName("异常描述")]
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 异常堆栈信息
|
||||
/// </summary>
|
||||
public string? StackTrace { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 日志时间戳
|
||||
/// </summary>
|
||||
[DisplayName("记录时间")]
|
||||
public DateTime LogTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 分类信息
|
||||
/// </summary>
|
||||
[DisplayName("分类信息")]
|
||||
public string? Category { get; set; }
|
||||
}
|
|
@ -73,6 +73,7 @@ public static class ServicesExtensions
|
|||
services.AddSingleton<IRole, RoleService>();
|
||||
services.AddSingleton<IGroup, GroupService>();
|
||||
services.AddSingleton<IApp, AppService>();
|
||||
services.AddSingleton<IException, ExceptionService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
using BootstrapAdmin.Web.Core;
|
||||
|
||||
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
|
||||
|
||||
class ExceptionService : IException
|
||||
{
|
||||
public bool Log(Models.Exception exception)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
namespace BootstrapAdmin.Web.Core;
|
||||
|
||||
public interface IException
|
||||
{
|
||||
bool Log(DataAccess.Models.Exception exception);
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
using System.Collections.Specialized;
|
||||
using BootstrapAdmin.Web.Core;
|
||||
using System.Collections.Specialized;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace BootstrapAdmin.Web.Utils;
|
||||
|
||||
|
@ -16,6 +18,34 @@ public static class ExceptionsHelper
|
|||
/// <returns></returns>
|
||||
public static void Log(IServiceProvider provider, Exception? ex, NameValueCollection additionalInfo)
|
||||
{
|
||||
|
||||
if (ex != null)
|
||||
{
|
||||
var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name);
|
||||
var loopEx = ex;
|
||||
var category = "App";
|
||||
while (loopEx != null)
|
||||
{
|
||||
if (typeof(DbException).IsAssignableFrom(loopEx.GetType()))
|
||||
{
|
||||
category = "DB";
|
||||
break;
|
||||
}
|
||||
loopEx = loopEx.InnerException;
|
||||
}
|
||||
var exception = new DataAccess.Models.Exception
|
||||
{
|
||||
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,
|
||||
Category = category
|
||||
};
|
||||
var expceptionService = provider.GetRequiredService<IException>();
|
||||
expceptionService.Log(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ namespace Bootstrap.DataAccess
|
|||
[TableName("DBLogs")]
|
||||
public class DBLog
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 主键ID
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue