ปรับปรุงการเก็บ log

This commit is contained in:
LAPTOP-KB8JC2K2\acer
2021-05-19 21:25:40 +07:00
parent 9f106272c8
commit 0a104bc9c4
7 changed files with 1293 additions and 5 deletions

View File

@@ -45,6 +45,9 @@ namespace TTSW.EF {
public DbSet<eva_create_evaluation_detail_historyEntity> eva_create_evaluation_detail_history { get; set; }
public DbSet<eva_evaluation_achievement_attachEntity> eva_evaluation_achievement_attach { get; set; }
public DbSet<activity_log_evaEntity> activity_log_eva { get; set; }
protected override void OnModelCreating (ModelBuilder modelBuilder) {
base.OnModelCreating (modelBuilder);

View File

@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Transactions;
using AutoMapper;
using System.Linq.Expressions;
using TTSW.Common;
@@ -211,6 +209,26 @@ namespace TTSW.EF
entity = SetUpdatorProperties(entity);
entity = SetActiveProperty(entity);
var log = new activity_log();
log.attributes = entity;
log.old = null;
var log_message = Newtonsoft.Json.JsonConvert.SerializeObject(log);
var activity_log = new activity_log_evaEntity();
activity_log.log_name = entity.GetType().Name;
activity_log.description = "Add " + entity.GetType().Name;
activity_log.subject_id = 0;
activity_log.subject_type = entity.GetType().Name;
activity_log.causer_id = Convert.ToInt32(_contextAccessor.HttpContext.Request.Cookies["user_id"]);
activity_log.causer_type = @"App\Models\User";
activity_log.properties = log_message;
activity_log.ip_address = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
activity_log.mac_address = "N/A";
activity_log.created_at = DateTime.Now;
activity_log.updated_at = activity_log.created_at;
_context.Add(activity_log);
var result = Entities.Add(entity);
if(!SaveToDB())
@@ -242,12 +260,32 @@ namespace TTSW.EF
var userLogin = GetLoginProfile();
var existingItem = Get(id);
var old_existingItem = Get(id);
if(existingItem == null)
if (existingItem == null)
throw new NotificationException($"No item in database.");
Mapper.Map(model, existingItem);
var log = new activity_log();
log.attributes = existingItem;
log.old = old_existingItem;
var log_message = Newtonsoft.Json.JsonConvert.SerializeObject(log);
var activity_log = new activity_log_evaEntity();
activity_log.log_name = existingItem.GetType().Name;
activity_log.description = "update " + existingItem.GetType().Name;
activity_log.subject_id = 0;
activity_log.subject_type = existingItem.GetType().Name;
activity_log.causer_id = Convert.ToInt32(_contextAccessor.HttpContext.Request.Cookies["user_id"]);
activity_log.causer_type = @"App\Models\User";
activity_log.properties = log_message;
activity_log.ip_address = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
activity_log.mac_address = "N/A";
activity_log.created_at = DateTime.Now;
activity_log.updated_at = activity_log.created_at;
_context.Add(activity_log);
existingItem = SetUpdatorProperties(existingItem);
if (!SaveToDB())

View File

@@ -10,9 +10,16 @@ using TTSW.Common;
using TodoAPI2.Models;
using Microsoft.AspNetCore.Http;
using IdentityModel;
using Newtonsoft.Json.Serialization;
namespace TTSW.EF
{
public class activity_log
{
public object attributes { get; set; }
public object old { get; set; }
}
public class BaseRepository2<T, Key> : IBaseRepository2<T, Key>
where T : class, IBaseEntity2<Key>
{
@@ -213,7 +220,27 @@ namespace TTSW.EF
var result = Entities.Add(entity);
if(!SaveToDB())
var log = new activity_log();
log.attributes = entity;
log.old = null;
var log_message = Newtonsoft.Json.JsonConvert.SerializeObject(log);
var activity_log = new activity_log_evaEntity();
activity_log.log_name = entity.GetType().Name;
activity_log.description = "Add " + entity.GetType().Name;
activity_log.subject_id = 0;
activity_log.subject_type = entity.GetType().Name;
activity_log.causer_id = Convert.ToInt32(_contextAccessor.HttpContext.Request.Cookies["user_id"]);
activity_log.causer_type = @"App\Models\User";
activity_log.properties = log_message;
activity_log.ip_address = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
activity_log.mac_address = "N/A";
activity_log.created_at = DateTime.Now;
activity_log.updated_at = activity_log.created_at;
_context.Add(activity_log);
if (!SaveToDB())
throw new NotificationException($"Unable to add new item to database.");
return Get(result.Entity.id);
@@ -242,12 +269,32 @@ namespace TTSW.EF
var userLogin = GetLoginProfile();
var existingItem = Get(id);
var old_existingItem = Get(id);
if(existingItem == null)
if (existingItem == null)
throw new NotificationException($"No item in database.");
Mapper.Map(model, existingItem);
var log = new activity_log();
log.attributes = existingItem;
log.old = old_existingItem;
var log_message = Newtonsoft.Json.JsonConvert.SerializeObject(log);
var activity_log = new activity_log_evaEntity();
activity_log.log_name = existingItem.GetType().Name;
activity_log.description = "update " + existingItem.GetType().Name;
activity_log.subject_id = 0;
activity_log.subject_type = existingItem.GetType().Name;
activity_log.causer_id = Convert.ToInt32(_contextAccessor.HttpContext.Request.Cookies["user_id"]);
activity_log.causer_type = @"App\Models\User";
activity_log.properties = log_message;
activity_log.ip_address = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
activity_log.mac_address = "N/A";
activity_log.created_at = DateTime.Now;
activity_log.updated_at = activity_log.created_at;
_context.Add(activity_log);
existingItem = SetUpdatorProperties(existingItem);
if (!SaveToDB())