Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.SignalR;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
|
||||
public class ActionChangeBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly ActionChangeChannel _channel;
|
||||
public ActionChangeBackgroundService(ActionChangeChannel channel)
|
||||
{
|
||||
_channel = channel;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await _channel.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
124
api_sqlsugar/VolPro.Core/Log/ActionChangeChannel.cs
Normal file
124
api_sqlsugar/VolPro.Core/Log/ActionChangeChannel.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
using VolPro.Core.EFDbContext;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Core.ManageUser;
|
||||
using VolPro.Core.Services;
|
||||
using VolPro.Core.SignalR;
|
||||
using VolPro.Core.WeChat;
|
||||
using VolPro.Entity.DomainModels;
|
||||
using Logger = VolPro.Core.Services.Logger;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
public class ActionChangeChannel
|
||||
{
|
||||
private bool IsStarted = false;
|
||||
private readonly Channel<ActionChangeQueue> _actionChannel = Channel.CreateUnbounded<ActionChangeQueue>();
|
||||
public async Task Run()
|
||||
{
|
||||
Console.WriteLine("审计日志服务已启动");
|
||||
IsStarted = true;
|
||||
try
|
||||
{
|
||||
while (await _actionChannel.Reader.WaitToReadAsync())
|
||||
{
|
||||
if (_actionChannel.Reader.TryRead(out ActionChangeQueue channelData))
|
||||
{
|
||||
var list = channelData.Datas.Select(s => new Sys_ActionLog()
|
||||
{
|
||||
ActionLogId = Guid.NewGuid(),
|
||||
TableName = channelData.TableName,
|
||||
TableCNName = channelData.TableCNName,
|
||||
OriginalData = s.OriginalData.Serialize(),
|
||||
ChangedData = s.ChangedData.Serialize(),
|
||||
DbServiceId = channelData.DbServiceId,
|
||||
TenancyId = channelData.DbServiceId?.ToString(),
|
||||
RequestParameter = channelData.RequestParameter,
|
||||
OperationType = channelData.ActionChangeType.ToString(),
|
||||
CreateID = channelData.CreateID,
|
||||
CreateDate = channelData.CreateDate,
|
||||
Creator = channelData.Creator
|
||||
}).ToList();
|
||||
DbManger.SysDbContext.Insertable<Sys_ActionLog>(list).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsStarted = false;
|
||||
string messg = $"日志审计队列异常:{ex.Message},{ex?.StackTrace}";
|
||||
Logger.AddAsync(messg);
|
||||
}
|
||||
}
|
||||
public void Write<T>(ActionChangeType changeType, T originalData, T changedData,string requestParameter=null) where T : class
|
||||
{
|
||||
if (!CheckIsStarted()) return;
|
||||
_actionChannel.Writer.TryWrite(GetQueueData<T>(changeType, originalData, changedData,requestParameter));
|
||||
}
|
||||
public async Task WriteAsync<T>(ActionChangeType changeType, T originalData, T changedData, string requestParameter = null) where T : class
|
||||
{
|
||||
if (!CheckIsStarted()) return;
|
||||
await _actionChannel.Writer.WriteAsync(GetQueueData<T>(changeType, originalData, changedData,requestParameter));
|
||||
}
|
||||
public void WriteRange<T>(ActionChangeType changeType, List<T> originalData, string requestParameter = null) where T : class
|
||||
{
|
||||
if (!CheckIsStarted()) return;
|
||||
var userInfo = UserContext.Current.UserInfo;
|
||||
var data = new ActionChangeQueue()
|
||||
{
|
||||
ActionChangeType = changeType,
|
||||
TableName = typeof(T).GetEntityTableName(),
|
||||
TableCNName = typeof(T).GetEntityTableCnName(),
|
||||
CreateID = userInfo.User_Id,
|
||||
CreateDate = DateTime.Now,
|
||||
Creator = userInfo.UserTrueName,
|
||||
DbServiceId = UserContext.CurrentServiceId,
|
||||
RequestParameter=requestParameter,
|
||||
Datas = originalData.Select(s => new ActionChangeData()
|
||||
{
|
||||
OriginalData = s
|
||||
}).ToList()
|
||||
};
|
||||
_actionChannel.Writer.TryWrite(data);
|
||||
}
|
||||
private ActionChangeQueue GetQueueData<T>(ActionChangeType changeType, object originalData, object changedData = null, string requestParameter = null) where T : class
|
||||
{
|
||||
var userInfo = UserContext.Current.UserInfo;
|
||||
return new ActionChangeQueue()
|
||||
{
|
||||
ActionChangeType = changeType,
|
||||
TableName = typeof(T).GetEntityTableName(),
|
||||
TableCNName = typeof(T).GetEntityTableCnName(),
|
||||
CreateID = userInfo.User_Id,
|
||||
CreateDate = DateTime.Now,
|
||||
Creator = userInfo.UserTrueName,
|
||||
RequestParameter = requestParameter,
|
||||
DbServiceId = UserContext.CurrentServiceId,
|
||||
Datas = new List<ActionChangeData>()
|
||||
{
|
||||
new(){
|
||||
OriginalData= originalData,
|
||||
ChangedData= changedData
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
private bool CheckIsStarted()
|
||||
{
|
||||
if (!IsStarted)
|
||||
{
|
||||
Console.WriteLine("审计日志服务未启动,请按更新文档在Program.cs或Startup.cs中开启");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
68
api_sqlsugar/VolPro.Core/Log/ActionChangeData.cs
Normal file
68
api_sqlsugar/VolPro.Core/Log/ActionChangeData.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
public class ActionChangeQueue {
|
||||
public ActionChangeType ActionChangeType { get;set;}
|
||||
|
||||
public List<ActionChangeData> Datas { get; set;}
|
||||
/// <summary>
|
||||
///数据库表
|
||||
/// </summary>
|
||||
[Display(Name = "数据库表")]
|
||||
[MaxLength(100)]
|
||||
[Column(TypeName = "nvarchar(100)")]
|
||||
[Editable(true)]
|
||||
public string TableName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///业务表名
|
||||
/// </summary>
|
||||
[Display(Name = "业务表名")]
|
||||
[MaxLength(128)]
|
||||
[Column(TypeName = "nvarchar(128)")]
|
||||
[Editable(true)]
|
||||
public string TableCNName { get; set; }
|
||||
|
||||
public Guid? DbServiceId { get; set; }
|
||||
|
||||
public string RequestParameter { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Display(Name = "CreateID")]
|
||||
[Column(TypeName = "int")]
|
||||
[Editable(true)]
|
||||
public int? CreateID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///创建人
|
||||
/// </summary>
|
||||
[Display(Name = "创建人")]
|
||||
[MaxLength(100)]
|
||||
[Column(TypeName = "nvarchar(100)")]
|
||||
[Editable(true)]
|
||||
public string Creator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///创建时间
|
||||
/// </summary>
|
||||
[Display(Name = "创建时间")]
|
||||
[Column(TypeName = "datetime")]
|
||||
[Editable(true)]
|
||||
public DateTime? CreateDate { get; set; }
|
||||
}
|
||||
|
||||
public class ActionChangeData
|
||||
{
|
||||
public object OriginalData { get; set; }
|
||||
public object ChangedData { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
29
api_sqlsugar/VolPro.Core/Log/ActionChangeExtensions.cs
Normal file
29
api_sqlsugar/VolPro.Core/Log/ActionChangeExtensions.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.SignalR;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
public static class ActionChangeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册审计日志
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddActionLog(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<ActionChangeChannel>();
|
||||
services.AddScoped<ActionChangeLog>();
|
||||
services.AddHostedService<ActionChangeBackgroundService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
api_sqlsugar/VolPro.Core/Log/ActionChangeLog.cs
Normal file
26
api_sqlsugar/VolPro.Core/Log/ActionChangeLog.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
public class ActionChangeLog(ActionChangeChannel changeChannel)
|
||||
{
|
||||
private readonly ActionChangeChannel _changeChannel = changeChannel;
|
||||
|
||||
public void Write<T>(ActionChangeType changeType, T originalData, T changedData=null, string requestParameter = null) where T : class
|
||||
{
|
||||
_changeChannel.Write(changeType, originalData, changedData,requestParameter);
|
||||
}
|
||||
public async Task WriteAsync<T>(ActionChangeType changeType, T originalData, T changedData=null, string requestParameter = null) where T : class
|
||||
{
|
||||
await _changeChannel.WriteAsync(changeType, originalData, changedData,requestParameter);
|
||||
}
|
||||
public void WriteRange<T>(ActionChangeType changeType, List<T> originalData, string requestParameter = null) where T : class
|
||||
{
|
||||
_changeChannel.WriteRange(changeType, originalData,requestParameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
api_sqlsugar/VolPro.Core/Log/ActionChangeType.cs
Normal file
15
api_sqlsugar/VolPro.Core/Log/ActionChangeType.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.Log
|
||||
{
|
||||
public enum ActionChangeType
|
||||
{
|
||||
Add,
|
||||
Update,
|
||||
Delete
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user