✨ 新增差异日志记录到库
This commit is contained in:
@@ -2,6 +2,7 @@ using Infrastructure;
|
||||
using Infrastructure.Extensions;
|
||||
using SqlSugar;
|
||||
using SqlSugar.IOC;
|
||||
using System.Security.Principal;
|
||||
using ZR.Model;
|
||||
using ZR.Model.System;
|
||||
|
||||
@@ -13,7 +14,7 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
public static class DbExtension
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化db
|
||||
/// </summary>
|
||||
@@ -93,7 +94,7 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
};
|
||||
db.GetConnectionScope(configId).Aop.OnError = (ex) =>
|
||||
{
|
||||
var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
|
||||
string sql = "【错误SQL】" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n";
|
||||
logger.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}");
|
||||
@@ -101,7 +102,44 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) =>
|
||||
{
|
||||
};
|
||||
//差异日志功能
|
||||
db.GetConnectionScope(configId).Aop.OnDiffLogEvent = it =>
|
||||
{
|
||||
//操作前记录 包含: 字段描述 列名 值 表名 表描述
|
||||
var editBeforeData = it.BeforeData;//插入Before为null,之前还没进库
|
||||
//操作后记录 包含: 字段描述 列名 值 表名 表描述
|
||||
var editAfterData = it.AfterData;
|
||||
var sql = it.Sql;
|
||||
var parameter = it.Parameters;
|
||||
var data = it.BusinessData;//这边会显示你传进来的对象
|
||||
var time = it.Time;
|
||||
var diffType = it.DiffType;//enum insert 、update and delete
|
||||
|
||||
if (diffType == DiffType.delete)
|
||||
{
|
||||
string name = App.UserName;
|
||||
|
||||
foreach (var item in editBeforeData)
|
||||
{
|
||||
var pars = db.Utilities.SerializeObject(item.Columns.ToDictionary(it => it.ColumnName, it => it.Value));
|
||||
|
||||
SqlDiffLog log = new()
|
||||
{
|
||||
AfterData = pars,
|
||||
BusinessData = data,
|
||||
DiffType = diffType.ToString(),
|
||||
Sql = sql,
|
||||
TableName = item.TableName,
|
||||
UserName = name,
|
||||
AddTime = DateTime.Now
|
||||
};
|
||||
//logger.WithProperty("title", data).Info(pars);
|
||||
db.GetConnectionScope(configId).Insertable(log).ExecuteReturnSnowflakeId();
|
||||
}
|
||||
}
|
||||
|
||||
//Write logic
|
||||
};
|
||||
db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true
|
||||
|
||||
@@ -164,6 +164,36 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
return context != null ? context.Request.QueryString.Value : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取body请求参数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetBody(this HttpContext context)
|
||||
{
|
||||
context.Request.EnableBuffering();
|
||||
//context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
//using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
|
||||
////需要使用异步方式才能获取
|
||||
//return reader.ReadToEndAsync().Result;
|
||||
string body = string.Empty;
|
||||
var buffer = new MemoryStream();
|
||||
context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
context.Request.Body.CopyToAsync(buffer);
|
||||
buffer.Position = 0;
|
||||
try
|
||||
{
|
||||
using StreamReader streamReader = new(buffer, Encoding.UTF8);
|
||||
body = streamReader.ReadToEndAsync().Result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
buffer?.Dispose();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置请求参数
|
||||
/// </summary>
|
||||
@@ -172,21 +202,14 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
public static void GetRequestValue(this HttpContext context, SysOperLog operLog)
|
||||
{
|
||||
string reqMethod = operLog.RequestMethod;
|
||||
string param;
|
||||
string param= string.Empty;
|
||||
|
||||
if (HttpMethods.IsPost(reqMethod) || HttpMethods.IsPut(reqMethod) || HttpMethods.IsDelete(reqMethod))
|
||||
{
|
||||
context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
|
||||
//需要使用异步方式才能获取
|
||||
param = reader.ReadToEndAsync().Result;
|
||||
if (param.IsEmpty())
|
||||
{
|
||||
param = context.GetQueryString();
|
||||
}
|
||||
param = context.GetBody();
|
||||
param = PwdRep().Replace(param, "***");
|
||||
}
|
||||
else
|
||||
if (param.IsEmpty())
|
||||
{
|
||||
param = context.GetQueryString();
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
//db.CodeFirst.InitTables(entityes);
|
||||
|
||||
//23个表,建议先使用下面方法初始化表,方便排查问题
|
||||
db.CodeFirst.InitTables(typeof(SqlDiffLog));
|
||||
db.CodeFirst.InitTables(typeof(SysUser));
|
||||
db.CodeFirst.InitTables(typeof(SysRole));
|
||||
db.CodeFirst.InitTables(typeof(SysDept));
|
||||
|
||||
Reference in New Issue
Block a user