RG3.BO.DB
bo_operation_oplog.xml
日志开启 “opLogDbType”: “MySql”
“opLogDbType”: “MySql”, //操作日志存储的数据库类别 MySql Oracle PostgreSql Sqlite Sqlserver
RG3.BO.DB.Providers OperateLogProvider
dictionaryUrl["_path"] = _accessor?.HttpContext?.Request?.Path.ToString();
_operateLog.Save(pfTemp, dictionaryUrl, JsonUtil.Serialize(sos.Data, false, false, false), JsonUtil.Serialize(jToken), JsonUtil.Serialize(new { query = RequestPFUtil.GetUrlQuery(_accessor.HttpContext) }, false, false, false), sos.SqlUpdate);
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using RG3.DO.Abstractions.Enums;
using RG3.DO.Abstractions.Interfaces;
using RG3.PF.Abstractions.Entity;
using RG3.PF.Abstractions.Enums;
using RG3.PF.Abstractions.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RG3.BO.DB.Providers
{
/// <summary>
///
/// </summary>
public class OperateLogProvider : IOperateLog
{
private IDbContext _db;
private IMapperProvider _mapper;
private IAuthProvider _auth;
/// <summary>
/// 配置
/// </summary>
private IConfiguration _configDev;
/// <summary>
/// 日志
/// </summary>
private readonly ILogger<OperateLogProvider> _logger;
/// <summary>
/// 注入服务
/// </summary>
/// <param name="db"></param>
public OperateLogProvider(IConfiguration configDev, ILogger<OperateLogProvider> logger, IDbContext db, IMapperProvider mapper, IAuthProvider auth)
{
_mapper = mapper;
_db = db;
_auth = auth;
_logger = logger;
_configDev = configDev;
}
/// <summary>
/// 保存信息
/// </summary>
/// <param name="pf"></param>
/// <param name="primaryId"></param>
/// <param name="bodyJson"></param>
/// <param name="queryJson"></param>
/// <param name="doSql">执行的sql语句,便于后期回复</param>
/// <returns></returns>
public async Task Save(PFGlobalParameter pf, Dictionary<string, object> dictionaryUrl, string primaryId, string bodyJson, string queryJson, string doSql)
{
try
{
////操作日志存储的数据库类别 mysql sqlserver oracle sqlite
string dataBaseType = _configDev.GetValue<string>("operation:opLogDbType");
if (string.IsNullOrEmpty(dataBaseType)) return;
//判断表是否存在,如果不存在添加
var mapper = _mapper.GetMapper(new PFGlobalParameter { OwnerId = pf.OwnerId, SysId = pf.SysId, DatabaseType = dataBaseType.ToString() }, "bo_operation_oplog.i_bo_op_log_table");
string sql = mapper.Sql.Replace("_@suffix", DateTime.Now.ToString("_yyyyMMdd"));
//动态创建表
var result = _db.Execute(pf, mapper.ConnName, sql, new object { });
//插入数据
var mapperD = _mapper.GetMapper(new PFGlobalParameter { OwnerId = pf.OwnerId, SysId = pf.SysId, DatabaseType = dataBaseType.ToString() }, "bo_operation_oplog.i_bo_op_log_data");
string sqlD = mapperD.Sql.Replace("_@suffix", DateTime.Now.ToString("_yyyyMMdd"));
object userId = dictionaryUrl.ContainsKey("_userId") ? dictionaryUrl["_userId"] : null;
object userName = dictionaryUrl.ContainsKey("_userName") ? dictionaryUrl["_userName"] : null;
try
{
userId = userId == null ? _auth.GetCurrentUserId(pf.OwnerId) : userId;
userName = userName == null ? _auth.GetCurrentToken(pf.OwnerId)?.PushName : userName;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "【OperateLogProvider用户获取错误】");
}
string dateT = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Dictionary<string, string> lstResult = new Dictionary<string, string>();
lstResult["modify"] = "局部更新";
lstResult["edit"] = "修改";
lstResult["add"] = "新增";
lstResult["delete"] = "删除";
lstResult["deletepost"] = "删除";
lstResult["null"] = "未知";
object logContent = $"{userName}在{dateT}对{pf.ModuleKey}模块执行了{lstResult[pf.RequestModuleType.ToString().ToLower()]}操作";
Dictionary<string, object> dicParams = new Dictionary<string, object>();
dicParams["areaCode"] = dictionaryUrl.ContainsKey("_areaCode") ? dictionaryUrl["_areaCode"] :null;
dicParams["clientIp"] = dictionaryUrl.ContainsKey("_clientUserIp") ? dictionaryUrl["_clientUserIp"] : null;
dicParams["createDate"] = DateTime.Now;
dicParams["createUserId"] = userId;
dicParams["createUserName"] = userName;
dicParams["fromAppId"] = dictionaryUrl.ContainsKey("_fromAppId") ? dictionaryUrl["_fromAppId"] : null;
dicParams["logBody"] = bodyJson;
dicParams["logContent"] = logContent;
dicParams["logId"] = dictionaryUrl.ContainsKey("_primaryIdV") ? dictionaryUrl["_primaryIdV"] : Guid.NewGuid().ToString();
dicParams["logParam"] = null;
dicParams["logPath"] = dictionaryUrl.ContainsKey("_path") ? dictionaryUrl["_path"] : null;
dicParams["logPrimayId"] = primaryId;
dicParams["logQuery"] = queryJson;
dicParams["logSql"] = doSql;
dicParams["logType"] = pf.RequestModuleType.ToString();
dicParams["moduleKey"] = pf.ModuleKey;
dicParams["ownerId"] = pf.OwnerId;
dicParams["responseField"] = pf.ReponseField;
dicParams["sysId"] = pf.SysId;
result = _db.Execute(pf, mapper.ConnName, sqlD, dicParams);
}
catch (Exception e)
{
_logger.LogWarning(e, "【OperateLogProvider存储错误】");
}
}
}
}
注入操作日志记录 Startup.cs
public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IOperateLog, BO.DB.Providers.OperateLogProvider>();
{ownerId}authoplog
文档更新时间: 2021-08-07 12:09 作者:admin