ปรับปรุงการเก็บ 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())

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace tb320eva.Migrations
{
public partial class AddLog : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "activity_log_eva",
columns: table => new
{
id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
log_name = table.Column<string>(maxLength: 191, nullable: true),
description = table.Column<string>(maxLength: 4000, nullable: true),
subject_id = table.Column<int>(nullable: true),
subject_type = table.Column<string>(maxLength: 191, nullable: true),
causer_id = table.Column<int>(nullable: true),
causer_type = table.Column<string>(maxLength: 191, nullable: true),
properties = table.Column<string>(maxLength: 8000, nullable: true),
ip_address = table.Column<string>(maxLength: 191, nullable: true),
mac_address = table.Column<string>(maxLength: 191, nullable: true),
created_at = table.Column<DateTime>(nullable: true),
updated_at = table.Column<DateTime>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_activity_log_eva", x => x.id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "activity_log_eva");
}
}
}

View File

@@ -19,6 +19,45 @@ namespace tb320eva.Migrations
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("TodoAPI2.Models.activity_log_evaEntity", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd();
b.Property<int?>("causer_id");
b.Property<string>("causer_type")
.HasMaxLength(191);
b.Property<DateTime?>("created_at");
b.Property<string>("description")
.HasMaxLength(4000);
b.Property<string>("ip_address")
.HasMaxLength(191);
b.Property<string>("log_name")
.HasMaxLength(191);
b.Property<string>("mac_address")
.HasMaxLength(191);
b.Property<string>("properties")
.HasMaxLength(8000);
b.Property<int?>("subject_id");
b.Property<string>("subject_type")
.HasMaxLength(191);
b.Property<DateTime?>("updated_at");
b.HasKey("id");
b.ToTable("activity_log_eva");
});
modelBuilder.Entity("TodoAPI2.Models.eva_adjust_postponementEntity", b =>
{
b.Property<int>("id");

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using TTSW.EF;
using TTSW.Utils;
using TTSW.Constant;
using TTSW.Common;
using System.IO;
namespace TodoAPI2.Models
{
public class activity_log_evaEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[MaxLength(191)]
public string log_name { get; set; }
[MaxLength(4000)]
public string description { get; set; }
public int? subject_id { get; set; }
[MaxLength(191)]
public string subject_type { get; set; }
public int? causer_id { get; set; }
[MaxLength(191)]
public string causer_type { get; set; }
[MaxLength(8000)]
public string properties { get; set; }
[MaxLength(191)]
public string ip_address { get; set; }
[MaxLength(191)]
public string mac_address { get; set; }
public DateTime? created_at { get; set; }
public DateTime? updated_at { get; set; }
}
}